← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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