← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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