← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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