← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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