← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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