← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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