← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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