← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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