← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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