← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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