← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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