← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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