← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

App development lesson 19: Loops over lists · Set 571 · 40 questions
TalentJR

Developer's tip (One by one): for (let i = 0; i < list.length; i++) visits every item, from position 0. Total: add each item. Count: add 1 when the test is true. Best: keep the biggest so far. The body runs once per item: a list of 5 runs it 5 times.

NameDateTime takenScore ___ / 40
  1. 1.🔁 A loop over a list of 5 items runs its body how many times? (a) 1 (b) 4 (c) 6 (d) 5
  2. 2.🔁 list.length tells you ___. (a) the last item (b) the biggest item (c) how many items it has (d) the first item
  3. 3.🔁 What is the LAST line this code shows?
    let names = ["Aman", "Riya", "Kabir"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Kabir (b) Hi, Aman (c) Hi, Riya
  4. 4.🔁 What is the LAST line this code shows?
    let names = ["Aman", "Zoya", "Arjun", "Meera"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Arjun (b) Hi, Zoya (c) Hi, Meera (d) Hi, Aman
  5. 5.🔁 What number does this code show?
    let steps = [6, 11, 16];
    let total = 0;
    for (let i = 0; i < steps.length; i++) {
      total = total + steps[i];
    }
    show(total);
  6. 6.🔁 What is the LAST line this code shows?
    let names = ["Aman", "Zoya", "Kabir", "Arjun"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Zoya (b) Hi, Arjun (c) Hi, Kabir (d) Hi, Aman
  7. 7.🔁 What number does this code show?
    let scores = [10, 12, 11, 18, 12];
    let best = 0;
    for (let i = 0; i < scores.length; i++) {
      if (scores[i] > best) {
        best = scores[i];
      }
    }
    show(best);
  8. 8.🔁 In for (let i = 0; i < 3; i++), which values does i take? (a) 0, 1, 2 (b) 0, 1, 2, 3 (c) 1, 2, 3 (d) 3
  9. 9.🔁 What number does this code show?
    let steps = [6, 7, 8, 15, 11];
    let best = 0;
    for (let i = 0; i < steps.length; i++) {
      if (steps[i] > best) {
        best = steps[i];
      }
    }
    show(best);
  10. 10.🔁 What number does this code show?
    let marks = [1, 12, 3];
    let total = 0;
    for (let i = 0; i < marks.length; i++) {
      total = total + marks[i];
    }
    show(total);
  11. 11.🔁 What number does this code show?
    let steps = [4, 7, 4];
    let total = 0;
    for (let i = 0; i < steps.length; i++) {
      total = total + steps[i];
    }
    show(total);
  12. 12.🔁 What is the LAST line this code shows?
    let names = ["Zoya", "Kabir", "Arjun", "Aman"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Zoya (b) Hi, Arjun (c) Hi, Kabir (d) Hi, Aman
  13. 13.🔁 What number does this code show?
    let scores = [11, 77, 59, 56, 82];
    let count = 0;
    for (let i = 0; i < scores.length; i++) {
      if (scores[i] >= 40) {
        count = count + 1;
      }
    }
    show(count);
  14. 14.🔁 What number does this code show?
    let scores = [9, 13, 6, 20, 14];
    let total = 0;
    for (let i = 0; i < scores.length; i++) {
      total = total + scores[i];
    }
    show(total);
  15. 15.🔁 What number does this code show?
    let runs = [19, 70, 52, 93, 22];
    let count = 0;
    for (let i = 0; i < runs.length; i++) {
      if (runs[i] >= 70) {
        count = count + 1;
      }
    }
    show(count);
  16. 16.🔁 What is the LAST line this code shows?
    let names = ["Arjun", "Aman", "Riya"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Aman (b) Hi, Arjun (c) Hi, Riya
  17. 17.🔁 What number does this code show?
    let steps = [6, 1, 19];
    let best = 0;
    for (let i = 0; i < steps.length; i++) {
      if (steps[i] > best) {
        best = steps[i];
      }
    }
    show(best);
  18. 18.🔁 What number does this code show?
    let steps = [9, 13, 19];
    let best = 0;
    for (let i = 0; i < steps.length; i++) {
      if (steps[i] > best) {
        best = steps[i];
      }
    }
    show(best);
  19. 19.🔁 What number does this code show?
    let steps = [17, 2, 4];
    let total = 0;
    for (let i = 0; i < steps.length; i++) {
      total = total + steps[i];
    }
    show(total);
  20. 20.🔁 What is the LAST line this code shows?
    let names = ["Riya", "Zoya", "Arjun", "Meera"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Riya (b) Hi, Meera (c) Hi, Zoya (d) Hi, Arjun
  21. 21.🔁 What is the LAST line this code shows?
    let names = ["Meera", "Riya", "Aman"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Meera (b) Hi, Riya (c) Hi, Aman
  22. 22.🔁 What number does this code show?
    let scores = [13, 3, 1, 13];
    let total = 0;
    for (let i = 0; i < scores.length; i++) {
      total = total + scores[i];
    }
    show(total);
  23. 23.🔁 What is the LAST line this code shows?
    let names = ["Meera", "Kabir", "Riya"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Kabir (b) Hi, Riya (c) Hi, Meera
  24. 24.🔁 What number does this code show?
    let marks = [13, 5, 15];
    let total = 0;
    for (let i = 0; i < marks.length; i++) {
      total = total + marks[i];
    }
    show(total);
  25. 25.🔁 What is the LAST line this code shows?
    let names = ["Arjun", "Meera", "Riya"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Arjun (b) Hi, Meera (c) Hi, Riya
  26. 26.🔁 What is the LAST line this code shows?
    let names = ["Riya", "Aman", "Arjun", "Meera", "Kabir"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Aman (b) Hi, Riya (c) Hi, Kabir (d) Hi, Arjun
  27. 27.🔁 What number does this code show?
    let marks = [16, 20, 4, 17];
    let total = 0;
    for (let i = 0; i < marks.length; i++) {
      total = total + marks[i];
    }
    show(total);
  28. 28.🔁 What number does this code show?
    let runs = [2, 9, 1, 3];
    let best = 0;
    for (let i = 0; i < runs.length; i++) {
      if (runs[i] > best) {
        best = runs[i];
      }
    }
    show(best);
  29. 29.🔁 What number does this code show?
    let scores = [36, 84, 29];
    let count = 0;
    for (let i = 0; i < scores.length; i++) {
      if (scores[i] >= 50) {
        count = count + 1;
      }
    }
    show(count);
  30. 30.🔁 Why use a loop to show 100 messages? (a) it uses more lines (b) less code to write (c) it is slower (d) it hides them
  31. 31.🔁 What number does this code show?
    let marks = [11, 18, 5, 11];
    let total = 0;
    for (let i = 0; i < marks.length; i++) {
      total = total + marks[i];
    }
    show(total);
  32. 32.🔁 What number does this code show?
    let runs = [17, 17, 13];
    let best = 0;
    for (let i = 0; i < runs.length; i++) {
      if (runs[i] > best) {
        best = runs[i];
      }
    }
    show(best);
  33. 33.🔁 What number does this code show?
    let marks = [1, 2, 9, 10];
    let total = 0;
    for (let i = 0; i < marks.length; i++) {
      total = total + marks[i];
    }
    show(total);
  34. 34.🔁 What number does this code show?
    let steps = [18, 2, 20];
    let best = 0;
    for (let i = 0; i < steps.length; i++) {
      if (steps[i] > best) {
        best = steps[i];
      }
    }
    show(best);
  35. 35.🔁 What is the LAST line this code shows?
    let names = ["Kabir", "Riya", "Meera", "Aman", "Arjun"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Riya (b) Hi, Meera (c) Hi, Kabir (d) Hi, Arjun
  36. 36.🔁 What does a loop do? (a) repeats code (b) saves photos (c) draws icons (d) deletes code
  37. 37.🔁 What number does this code show?
    let steps = [19, 14, 9];
    let best = 0;
    for (let i = 0; i < steps.length; i++) {
      if (steps[i] > best) {
        best = steps[i];
      }
    }
    show(best);
  38. 38.🔁 What is the LAST line this code shows?
    let names = ["Meera", "Arjun", "Zoya", "Kabir", "Aman"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Zoya (b) Hi, Aman (c) Hi, Meera (d) Hi, Arjun
  39. 39.🔁 What number does this code show?
    let scores = [9, 15, 16, 4, 12];
    let total = 0;
    for (let i = 0; i < scores.length; i++) {
      total = total + scores[i];
    }
    show(total);
  40. 40.🔁 What is the LAST line this code shows?
    let names = ["Kabir", "Arjun", "Meera"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Arjun (b) Hi, Kabir (c) Hi, Meera

Answer key

  1. 5
  2. how many items it has
  3. Hi, Kabir
  4. Hi, Meera
  5. 33
  6. Hi, Arjun
  7. 18
  8. 0, 1, 2
  9. 15
  10. 16
  11. 15
  12. Hi, Aman
  13. 4
  14. 62
  15. 2
  16. Hi, Riya
  17. 19
  18. 19
  19. 23
  20. Hi, Meera
  21. Hi, Aman
  22. 30
  23. Hi, Riya
  24. 33
  25. Hi, Riya
  26. Hi, Kabir
  27. 57
  28. 9
  29. 1
  30. less code to write
  31. 45
  32. 17
  33. 22
  34. 20
  35. Hi, Arjun
  36. repeats code
  37. 19
  38. Hi, Aman
  39. 56
  40. Hi, Meera

Free app development lessons, practice and worksheets at talentjr.in/app-development-for-students