← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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