← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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