← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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