← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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