← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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