← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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