← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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