← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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