← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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