← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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