← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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