← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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