← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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