← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

App development lesson 19: Loops over lists · Set 425 · 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 scores = [14, 11, 7];
    let total = 0;
    for (let i = 0; i < scores.length; i++) {
      total = total + scores[i];
    }
    show(total);
  2. 2.🔁 What is the LAST line this code shows?
    let names = ["Meera", "Aman", "Zoya"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Meera (b) Hi, Zoya (c) Hi, Aman
  3. 3.🔁 What is the LAST line this code shows?
    let names = ["Riya", "Aman", "Arjun", "Kabir"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Aman (b) Hi, Arjun (c) Hi, Riya (d) Hi, Kabir
  4. 4.🔁 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
  5. 5.🔁 list.length tells you ___. (a) the last item (b) the biggest item (c) how many items it has (d) the first item
  6. 6.🔁 What is the LAST line this code shows?
    let names = ["Riya", "Kabir", "Meera"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Meera (b) Hi, Kabir (c) Hi, Riya
  7. 7.🔁 What number does this code show?
    let steps = [20, 8, 13, 15, 13];
    let total = 0;
    for (let i = 0; i < steps.length; i++) {
      total = total + steps[i];
    }
    show(total);
  8. 8.🔁 What number does this code show?
    let marks = [6, 6, 3, 13];
    let total = 0;
    for (let i = 0; i < marks.length; i++) {
      total = total + marks[i];
    }
    show(total);
  9. 9.🔁 What does a loop do? (a) saves photos (b) draws icons (c) deletes code (d) repeats code
  10. 10.🔁 A loop over a list of 5 items runs its body how many times? (a) 4 (b) 1 (c) 6 (d) 5
  11. 11.🔁 What number does this code show?
    let scores = [14, 1, 17, 14];
    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 = [33, 80, 72];
    let count = 0;
    for (let i = 0; i < steps.length; i++) {
      if (steps[i] >= 50) {
        count = count + 1;
      }
    }
    show(count);
  13. 13.🔁 What is the LAST line this code shows?
    let names = ["Kabir", "Riya", "Meera", "Arjun"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Arjun (b) Hi, Kabir (c) Hi, Meera (d) Hi, Riya
  14. 14.🔁 What number does this code show?
    let scores = [19, 17, 12];
    let total = 0;
    for (let i = 0; i < scores.length; i++) {
      total = total + scores[i];
    }
    show(total);
  15. 15.🔁 In for (let i = 0; i < 3; i++), which values does i take? (a) 1, 2, 3 (b) 0, 1, 2, 3 (c) 3 (d) 0, 1, 2
  16. 16.🔁 What is the LAST line this code shows?
    let names = ["Meera", "Arjun", "Riya"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Arjun (b) Hi, Meera (c) Hi, Riya
  17. 17.🔁 What number does this code show?
    let runs = [33, 23, 31];
    let count = 0;
    for (let i = 0; i < runs.length; i++) {
      if (runs[i] >= 70) {
        count = count + 1;
      }
    }
    show(count);
  18. 18.🔁 What is the LAST line this code shows?
    let names = ["Zoya", "Arjun", "Aman"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Aman (b) Hi, Arjun (c) Hi, Zoya
  19. 19.🔁 What number does this code show?
    let scores = [4, 19, 16, 2, 18];
    let total = 0;
    for (let i = 0; i < scores.length; i++) {
      total = total + scores[i];
    }
    show(total);
  20. 20.🔁 What number does this code show?
    let steps = [45, 84, 71, 22, 88];
    let count = 0;
    for (let i = 0; i < steps.length; i++) {
      if (steps[i] >= 40) {
        count = count + 1;
      }
    }
    show(count);
  21. 21.🔁 What is the LAST line this code shows?
    let names = ["Arjun", "Kabir", "Riya"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Arjun (b) Hi, Kabir (c) Hi, Riya
  22. 22.🔁 What number does this code show?
    let runs = [2, 13, 12, 5, 2];
    let total = 0;
    for (let i = 0; i < runs.length; i++) {
      total = total + runs[i];
    }
    show(total);
  23. 23.🔁 What number does this code show?
    let steps = [4, 3, 4];
    let total = 0;
    for (let i = 0; i < steps.length; i++) {
      total = total + steps[i];
    }
    show(total);
  24. 24.🔁 What is the LAST line this code shows?
    let names = ["Riya", "Aman", "Kabir"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Aman (b) Hi, Kabir (c) Hi, Riya
  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, Zoya (b) Hi, Meera (c) Hi, Riya (d) Hi, Kabir
  26. 26.🔁 What number does this code show?
    let scores = [87, 71, 56, 51, 77];
    let count = 0;
    for (let i = 0; i < scores.length; i++) {
      if (scores[i] >= 70) {
        count = count + 1;
      }
    }
    show(count);
  27. 27.🔁 What number does this code show?
    let scores = [82, 84, 31];
    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 marks = [66, 92, 72, 60];
    let count = 0;
    for (let i = 0; i < marks.length; i++) {
      if (marks[i] >= 70) {
        count = count + 1;
      }
    }
    show(count);
  29. 29.🔁 What is the LAST line this code shows?
    let names = ["Meera", "Arjun", "Zoya"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Arjun (b) Hi, Meera (c) Hi, Zoya
  30. 30.🔁 What number does this code show?
    let marks = [90, 12, 33];
    let count = 0;
    for (let i = 0; i < marks.length; i++) {
      if (marks[i] >= 50) {
        count = count + 1;
      }
    }
    show(count);
  31. 31.🔁 What number does this code show?
    let runs = [4, 17, 5, 3];
    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 marks = [1, 20, 17, 11];
    let best = 0;
    for (let i = 0; i < marks.length; i++) {
      if (marks[i] > best) {
        best = marks[i];
      }
    }
    show(best);
  33. 33.🔁 What number does this code show?
    let steps = [12, 96, 70];
    let count = 0;
    for (let i = 0; i < steps.length; i++) {
      if (steps[i] >= 50) {
        count = count + 1;
      }
    }
    show(count);
  34. 34.🔁 What number does this code show?
    let steps = [80, 10, 28];
    let count = 0;
    for (let i = 0; i < steps.length; i++) {
      if (steps[i] >= 60) {
        count = count + 1;
      }
    }
    show(count);
  35. 35.🔁 What is the LAST line this code shows?
    let names = ["Riya", "Zoya", "Aman", "Arjun"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Zoya (b) Hi, Riya (c) Hi, Arjun (d) Hi, Aman
  36. 36.🔁 What is the LAST line this code shows?
    let names = ["Zoya", "Aman", "Riya", "Meera"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Aman (b) Hi, Zoya (c) Hi, Meera (d) Hi, Riya
  37. 37.🔁 What number does this code show?
    let scores = [5, 2, 8, 3, 7];
    let total = 0;
    for (let i = 0; i < scores.length; i++) {
      total = total + scores[i];
    }
    show(total);
  38. 38.🔁 What is the LAST line this code shows?
    let names = ["Arjun", "Kabir", "Zoya", "Riya", "Meera"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Kabir (b) Hi, Meera (c) Hi, Zoya (d) Hi, Arjun
  39. 39.🔁 What number does this code show?
    let runs = [6, 14, 11, 20, 13];
    let best = 0;
    for (let i = 0; i < runs.length; i++) {
      if (runs[i] > best) {
        best = runs[i];
      }
    }
    show(best);
  40. 40.🔁 What number does this code show?
    let scores = [40, 71, 86];
    let count = 0;
    for (let i = 0; i < scores.length; i++) {
      if (scores[i] >= 60) {
        count = count + 1;
      }
    }
    show(count);

Answer key

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

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