← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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