← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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