← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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