← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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