← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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