← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

App development lesson 19: Loops over lists · Set 137 · 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 runs = [3, 2, 16, 1];
    let total = 0;
    for (let i = 0; i < runs.length; i++) {
      total = total + runs[i];
    }
    show(total);
  2. 2.🔁 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
  3. 3.🔁 What is the LAST line this code shows?
    let names = ["Kabir", "Arjun", "Aman"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Kabir (b) Hi, Arjun (c) Hi, Aman
  4. 4.🔁 What number does this code show?
    let scores = [86, 84, 35, 58];
    let count = 0;
    for (let i = 0; i < scores.length; i++) {
      if (scores[i] >= 40) {
        count = count + 1;
      }
    }
    show(count);
  5. 5.🔁 A loop over a list of 5 items runs its body how many times? (a) 1 (b) 6 (c) 5 (d) 4
  6. 6.🔁 What is the LAST line this code shows?
    let names = ["Aman", "Kabir", "Meera", "Zoya", "Riya"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Aman (b) Hi, Riya (c) Hi, Kabir (d) Hi, Meera
  7. 7.🔁 What number does this code show?
    let scores = [37, 18, 93, 15];
    let count = 0;
    for (let i = 0; i < scores.length; i++) {
      if (scores[i] >= 70) {
        count = count + 1;
      }
    }
    show(count);
  8. 8.🔁 What number does this code show?
    let scores = [49, 85, 19];
    let count = 0;
    for (let i = 0; i < scores.length; i++) {
      if (scores[i] >= 60) {
        count = count + 1;
      }
    }
    show(count);
  9. 9.🔁 What number does this code show?
    let runs = [36, 42, 84, 39];
    let count = 0;
    for (let i = 0; i < runs.length; i++) {
      if (runs[i] >= 40) {
        count = count + 1;
      }
    }
    show(count);
  10. 10.🔁 What is the LAST line this code shows?
    let names = ["Riya", "Meera", "Kabir", "Zoya"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Zoya (b) Hi, Riya (c) Hi, Meera (d) Hi, Kabir
  11. 11.🔁 What number does this code show?
    let marks = [5, 15, 16, 5];
    let total = 0;
    for (let i = 0; i < marks.length; i++) {
      total = total + marks[i];
    }
    show(total);
  12. 12.🔁 What is the LAST line this code shows?
    let names = ["Aman", "Zoya", "Riya", "Kabir", "Meera"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Zoya (b) Hi, Aman (c) Hi, Meera (d) Hi, Riya
  13. 13.🔁 What number does this code show?
    let scores = [8, 8, 7];
    let best = 0;
    for (let i = 0; i < scores.length; i++) {
      if (scores[i] > best) {
        best = scores[i];
      }
    }
    show(best);
  14. 14.🔁 What number does this code show?
    let marks = [66, 50, 39, 94];
    let count = 0;
    for (let i = 0; i < marks.length; i++) {
      if (marks[i] >= 50) {
        count = count + 1;
      }
    }
    show(count);
  15. 15.🔁 What number does this code show?
    let marks = [14, 4, 16];
    let total = 0;
    for (let i = 0; i < marks.length; i++) {
      total = total + marks[i];
    }
    show(total);
  16. 16.🔁 What number does this code show?
    let runs = [23, 97, 81, 74, 86];
    let count = 0;
    for (let i = 0; i < runs.length; i++) {
      if (runs[i] >= 40) {
        count = count + 1;
      }
    }
    show(count);
  17. 17.🔁 What number does this code show?
    let scores = [28, 37, 31];
    let count = 0;
    for (let i = 0; i < scores.length; i++) {
      if (scores[i] >= 40) {
        count = count + 1;
      }
    }
    show(count);
  18. 18.🔁 What is the LAST line this code shows?
    let names = ["Aman", "Arjun", "Kabir"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Aman (b) Hi, Kabir (c) Hi, Arjun
  19. 19.🔁 What does a loop do? (a) saves photos (b) draws icons (c) deletes code (d) repeats code
  20. 20.🔁 What number does this code show?
    let scores = [78, 34, 90, 13, 75];
    let count = 0;
    for (let i = 0; i < scores.length; i++) {
      if (scores[i] >= 40) {
        count = count + 1;
      }
    }
    show(count);
  21. 21.🔁 What is the LAST line this code shows?
    let names = ["Zoya", "Aman", "Kabir", "Riya"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Kabir (b) Hi, Aman (c) Hi, Riya (d) Hi, Zoya
  22. 22.🔁 What is the LAST line this code shows?
    let names = ["Zoya", "Kabir", "Aman", "Meera", "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
  23. 23.🔁 What is the LAST line this code shows?
    let names = ["Zoya", "Aman", "Arjun", "Riya"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Riya (b) Hi, Arjun (c) Hi, Aman (d) Hi, Zoya
  24. 24.🔁 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, Kabir (b) Hi, Meera (c) Hi, Arjun
  25. 25.🔁 What is the LAST line this code shows?
    let names = ["Riya", "Meera", "Aman", "Zoya"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Zoya (b) Hi, Riya (c) Hi, Meera (d) Hi, Aman
  26. 26.🔁 What number does this code show?
    let scores = [43, 47, 95, 27];
    let count = 0;
    for (let i = 0; i < scores.length; i++) {
      if (scores[i] >= 40) {
        count = count + 1;
      }
    }
    show(count);
  27. 27.🔁 What number does this code show?
    let runs = [12, 20, 9, 5, 4];
    let best = 0;
    for (let i = 0; i < runs.length; i++) {
      if (runs[i] > best) {
        best = runs[i];
      }
    }
    show(best);
  28. 28.🔁 What number does this code show?
    let steps = [13, 4, 6];
    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 scores = [8, 4, 6];
    let best = 0;
    for (let i = 0; i < scores.length; i++) {
      if (scores[i] > best) {
        best = scores[i];
      }
    }
    show(best);
  30. 30.🔁 What number does this code show?
    let scores = [41, 76, 87];
    let count = 0;
    for (let i = 0; i < scores.length; i++) {
      if (scores[i] >= 50) {
        count = count + 1;
      }
    }
    show(count);
  31. 31.🔁 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
  32. 32.🔁 What number does this code show?
    let runs = [16, 8, 11, 15, 7];
    let best = 0;
    for (let i = 0; i < runs.length; i++) {
      if (runs[i] > best) {
        best = runs[i];
      }
    }
    show(best);
  33. 33.🔁 What is the LAST line this code shows?
    let names = ["Kabir", "Riya", "Aman", "Arjun"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Kabir (b) Hi, Arjun (c) Hi, Aman (d) Hi, Riya
  34. 34.🔁 What number does this code show?
    let scores = [28, 53, 68, 75, 52];
    let count = 0;
    for (let i = 0; i < scores.length; i++) {
      if (scores[i] >= 40) {
        count = count + 1;
      }
    }
    show(count);
  35. 35.🔁 What number does this code show?
    let marks = [5, 10, 3, 14, 9];
    let total = 0;
    for (let i = 0; i < marks.length; i++) {
      total = total + marks[i];
    }
    show(total);
  36. 36.🔁 What number does this code show?
    let scores = [11, 6, 4, 19];
    let total = 0;
    for (let i = 0; i < scores.length; i++) {
      total = total + scores[i];
    }
    show(total);
  37. 37.🔁 What number does this code show?
    let scores = [84, 93, 55, 52, 83];
    let count = 0;
    for (let i = 0; i < scores.length; i++) {
      if (scores[i] >= 40) {
        count = count + 1;
      }
    }
    show(count);
  38. 38.🔁 What number does this code show?
    let runs = [1, 3, 16, 1];
    let total = 0;
    for (let i = 0; i < runs.length; i++) {
      total = total + runs[i];
    }
    show(total);
  39. 39.🔁 What is the LAST line this code shows?
    let names = ["Meera", "Aman", "Kabir", "Arjun"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Aman (b) Hi, Kabir (c) Hi, Arjun (d) Hi, Meera
  40. 40.🔁 What number does this code show?
    let scores = [6, 12, 2];
    let best = 0;
    for (let i = 0; i < scores.length; i++) {
      if (scores[i] > best) {
        best = scores[i];
      }
    }
    show(best);

Answer key

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

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