← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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