← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

App development lesson 19: Loops over lists · Set 822 · 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.🔁 What number does this code show?
    let scores = [13, 15, 16];
    let total = 0;
    for (let i = 0; i < scores.length; i++) {
      total = total + scores[i];
    }
    show(total);
  2. 2.🔁 What number does this code show?
    let steps = [7, 4, 13];
    let best = 0;
    for (let i = 0; i < steps.length; i++) {
      if (steps[i] > best) {
        best = steps[i];
      }
    }
    show(best);
  3. 3.🔁 In for (let i = 0; i < 3; i++), which values does i take? (a) 0, 1, 2 (b) 3 (c) 1, 2, 3 (d) 0, 1, 2, 3
  4. 4.🔁 What number does this code show?
    let runs = [19, 6, 20];
    let best = 0;
    for (let i = 0; i < runs.length; i++) {
      if (runs[i] > best) {
        best = runs[i];
      }
    }
    show(best);
  5. 5.🔁 What does a loop do? (a) draws icons (b) saves photos (c) deletes code (d) repeats code
  6. 6.🔁 What number does this code show?
    let marks = [9, 2, 17, 17, 11];
    let total = 0;
    for (let i = 0; i < marks.length; i++) {
      total = total + marks[i];
    }
    show(total);
  7. 7.🔁 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, Meera (b) Hi, Riya (c) Hi, Kabir
  8. 8.🔁 What number does this code show?
    let scores = [40, 41, 67];
    let count = 0;
    for (let i = 0; i < scores.length; i++) {
      if (scores[i] >= 60) {
        count = count + 1;
      }
    }
    show(count);
  9. 9.🔁 What number does this code show?
    let marks = [14, 9, 15, 18, 2];
    let total = 0;
    for (let i = 0; i < marks.length; i++) {
      total = total + marks[i];
    }
    show(total);
  10. 10.🔁 What is the LAST line this code shows?
    let names = ["Riya", "Arjun", "Kabir", "Meera"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Kabir (b) Hi, Riya (c) Hi, Meera (d) Hi, Arjun
  11. 11.🔁 What number does this code show?
    let scores = [20, 4, 6];
    let total = 0;
    for (let i = 0; i < scores.length; i++) {
      total = total + scores[i];
    }
    show(total);
  12. 12.🔁 list.length tells you ___. (a) the last item (b) the first item (c) the biggest item (d) how many items it has
  13. 13.🔁 What number does this code show?
    let steps = [5, 8, 5];
    let total = 0;
    for (let i = 0; i < steps.length; i++) {
      total = total + steps[i];
    }
    show(total);
  14. 14.🔁 What number does this code show?
    let steps = [2, 1, 12, 2];
    let best = 0;
    for (let i = 0; i < steps.length; i++) {
      if (steps[i] > best) {
        best = steps[i];
      }
    }
    show(best);
  15. 15.🔁 What number does this code show?
    let marks = [4, 2, 5, 10, 8];
    let best = 0;
    for (let i = 0; i < marks.length; i++) {
      if (marks[i] > best) {
        best = marks[i];
      }
    }
    show(best);
  16. 16.🔁 What is the LAST line this code shows?
    let names = ["Arjun", "Meera", "Riya", "Aman"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Riya (b) Hi, Aman (c) Hi, Arjun (d) Hi, Meera
  17. 17.🔁 What number does this code show?
    let steps = [4, 18, 1, 20];
    let best = 0;
    for (let i = 0; i < steps.length; i++) {
      if (steps[i] > best) {
        best = steps[i];
      }
    }
    show(best);
  18. 18.🔁 What is the LAST line this code shows?
    let names = ["Aman", "Arjun", "Riya", "Kabir", "Meera"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Meera (b) Hi, Aman (c) Hi, Riya (d) Hi, Arjun
  19. 19.🔁 What number does this code show?
    let marks = [10, 9, 17, 8];
    let best = 0;
    for (let i = 0; i < marks.length; i++) {
      if (marks[i] > best) {
        best = marks[i];
      }
    }
    show(best);
  20. 20.🔁 What number does this code show?
    let scores = [26, 13, 30];
    let count = 0;
    for (let i = 0; i < scores.length; i++) {
      if (scores[i] >= 60) {
        count = count + 1;
      }
    }
    show(count);
  21. 21.🔁 What number does this code show?
    let steps = [37, 44, 79];
    let count = 0;
    for (let i = 0; i < steps.length; i++) {
      if (steps[i] >= 60) {
        count = count + 1;
      }
    }
    show(count);
  22. 22.🔁 What number does this code show?
    let marks = [4, 3, 11, 17, 14];
    let total = 0;
    for (let i = 0; i < marks.length; i++) {
      total = total + marks[i];
    }
    show(total);
  23. 23.🔁 What number does this code show?
    let marks = [38, 94, 31, 34];
    let count = 0;
    for (let i = 0; i < marks.length; i++) {
      if (marks[i] >= 50) {
        count = count + 1;
      }
    }
    show(count);
  24. 24.🔁 What number does this code show?
    let runs = [3, 14, 6, 3, 11];
    let best = 0;
    for (let i = 0; i < runs.length; i++) {
      if (runs[i] > best) {
        best = runs[i];
      }
    }
    show(best);
  25. 25.🔁 What number does this code show?
    let scores = [11, 18, 6, 7];
    let best = 0;
    for (let i = 0; i < scores.length; i++) {
      if (scores[i] > best) {
        best = scores[i];
      }
    }
    show(best);
  26. 26.🔁 What is the LAST line this code shows?
    let names = ["Riya", "Arjun", "Meera"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Riya (b) Hi, Arjun (c) Hi, Meera
  27. 27.🔁 What number does this code show?
    let marks = [14, 3, 2, 5, 6];
    let best = 0;
    for (let i = 0; i < marks.length; i++) {
      if (marks[i] > best) {
        best = marks[i];
      }
    }
    show(best);
  28. 28.🔁 What number does this code show?
    let marks = [6, 4, 9];
    let best = 0;
    for (let i = 0; i < marks.length; i++) {
      if (marks[i] > best) {
        best = marks[i];
      }
    }
    show(best);
  29. 29.🔁 What number does this code show?
    let scores = [3, 9, 3, 6, 14];
    let total = 0;
    for (let i = 0; i < scores.length; i++) {
      total = total + scores[i];
    }
    show(total);
  30. 30.🔁 A loop over a list of 5 items runs its body how many times? (a) 1 (b) 5 (c) 6 (d) 4
  31. 31.🔁 What number does this code show?
    let scores = [66, 46, 16];
    let count = 0;
    for (let i = 0; i < scores.length; i++) {
      if (scores[i] >= 50) {
        count = count + 1;
      }
    }
    show(count);
  32. 32.🔁 What number does this code show?
    let runs = [2, 11, 11, 1, 7];
    let total = 0;
    for (let i = 0; i < runs.length; i++) {
      total = total + runs[i];
    }
    show(total);
  33. 33.🔁 What number does this code show?
    let marks = [21, 72, 94, 16];
    let count = 0;
    for (let i = 0; i < marks.length; i++) {
      if (marks[i] >= 70) {
        count = count + 1;
      }
    }
    show(count);
  34. 34.🔁 What number does this code show?
    let marks = [14, 10, 13];
    let total = 0;
    for (let i = 0; i < marks.length; i++) {
      total = total + marks[i];
    }
    show(total);
  35. 35.🔁 What number does this code show?
    let scores = [15, 3, 5, 12, 16];
    let total = 0;
    for (let i = 0; i < scores.length; i++) {
      total = total + scores[i];
    }
    show(total);
  36. 36.🔁 What number does this code show?
    let scores = [15, 2, 19, 6];
    let total = 0;
    for (let i = 0; i < scores.length; i++) {
      total = total + scores[i];
    }
    show(total);
  37. 37.🔁 What number does this code show?
    let runs = [11, 20, 14, 6, 18];
    let best = 0;
    for (let i = 0; i < runs.length; i++) {
      if (runs[i] > best) {
        best = runs[i];
      }
    }
    show(best);
  38. 38.🔁 What number does this code show?
    let scores = [19, 12, 50, 65];
    let count = 0;
    for (let i = 0; i < scores.length; i++) {
      if (scores[i] >= 60) {
        count = count + 1;
      }
    }
    show(count);
  39. 39.🔁 What number does this code show?
    let steps = [3, 19, 20];
    let total = 0;
    for (let i = 0; i < steps.length; i++) {
      total = total + steps[i];
    }
    show(total);
  40. 40.🔁 Why use a loop to show 100 messages? (a) it is slower (b) it uses more lines (c) it hides them (d) less code to write

Answer key

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

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