← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

App development lesson 19: Loops over lists · Set 397 · 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) deletes code (c) saves photos (d) draws icons
  2. 2.🔁 What number does this code show?
    let steps = [7, 8, 19, 19, 12];
    let total = 0;
    for (let i = 0; i < steps.length; i++) {
      total = total + steps[i];
    }
    show(total);
  3. 3.🔁 What number does this code show?
    let marks = [6, 6, 3, 4, 13];
    let best = 0;
    for (let i = 0; i < marks.length; i++) {
      if (marks[i] > best) {
        best = marks[i];
      }
    }
    show(best);
  4. 4.🔁 What number does this code show?
    let scores = [13, 83, 76];
    let count = 0;
    for (let i = 0; i < scores.length; i++) {
      if (scores[i] >= 70) {
        count = count + 1;
      }
    }
    show(count);
  5. 5.🔁 What is the LAST line this code shows?
    let names = ["Meera", "Zoya", "Arjun"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Arjun (b) Hi, Zoya (c) Hi, Meera
  6. 6.🔁 What is the LAST line this code shows?
    let names = ["Meera", "Aman", "Riya"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Riya (b) Hi, Meera (c) Hi, Aman
  7. 7.🔁 What number does this code show?
    let scores = [15, 9, 19, 13, 3];
    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 = [2, 14, 14];
    let best = 0;
    for (let i = 0; i < runs.length; i++) {
      if (runs[i] > best) {
        best = runs[i];
      }
    }
    show(best);
  9. 9.🔁 A loop over a list of 5 items runs its body how many times? (a) 4 (b) 1 (c) 5 (d) 6
  10. 10.🔁 What number does this code show?
    let marks = [7, 5, 4, 15];
    let total = 0;
    for (let i = 0; i < marks.length; i++) {
      total = total + marks[i];
    }
    show(total);
  11. 11.🔁 What number does this code show?
    let steps = [11, 12, 11];
    let best = 0;
    for (let i = 0; i < steps.length; i++) {
      if (steps[i] > best) {
        best = steps[i];
      }
    }
    show(best);
  12. 12.🔁 What number does this code show?
    let runs = [9, 20, 17, 9, 5];
    let best = 0;
    for (let i = 0; i < runs.length; i++) {
      if (runs[i] > best) {
        best = runs[i];
      }
    }
    show(best);
  13. 13.🔁 What number does this code show?
    let scores = [19, 2, 11, 17];
    let total = 0;
    for (let i = 0; i < scores.length; i++) {
      total = total + scores[i];
    }
    show(total);
  14. 14.🔁 What is the LAST line this code shows?
    let names = ["Aman", "Zoya", "Meera", "Arjun"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Meera (b) Hi, Zoya (c) Hi, Arjun (d) Hi, Aman
  15. 15.🔁 What number does this code show?
    let marks = [19, 15, 1, 15];
    let best = 0;
    for (let i = 0; i < marks.length; i++) {
      if (marks[i] > best) {
        best = marks[i];
      }
    }
    show(best);
  16. 16.🔁 What number does this code show?
    let runs = [41, 93, 54, 25, 62];
    let count = 0;
    for (let i = 0; i < runs.length; i++) {
      if (runs[i] >= 70) {
        count = count + 1;
      }
    }
    show(count);
  17. 17.🔁 What is the LAST line this code shows?
    let names = ["Aman", "Riya", "Kabir", "Arjun"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Arjun (b) Hi, Riya (c) Hi, Aman (d) Hi, Kabir
  18. 18.🔁 What number does this code show?
    let scores = [3, 17, 11, 11, 9];
    let total = 0;
    for (let i = 0; i < scores.length; i++) {
      total = total + scores[i];
    }
    show(total);
  19. 19.🔁 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, Riya (b) Hi, Aman (c) Hi, Arjun
  20. 20.🔁 What number does this code show?
    let steps = [15, 7, 3, 15, 2];
    let total = 0;
    for (let i = 0; i < steps.length; i++) {
      total = total + steps[i];
    }
    show(total);
  21. 21.🔁 list.length tells you ___. (a) the biggest item (b) the first item (c) the last item (d) how many items it has
  22. 22.🔁 What is the LAST line this code shows?
    let names = ["Kabir", "Aman", "Arjun", "Riya", "Meera"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Aman (b) Hi, Meera (c) Hi, Kabir (d) Hi, Arjun
  23. 23.🔁 What number does this code show?
    let steps = [14, 2, 7, 10, 9];
    let best = 0;
    for (let i = 0; i < steps.length; i++) {
      if (steps[i] > best) {
        best = steps[i];
      }
    }
    show(best);
  24. 24.🔁 What number does this code show?
    let scores = [7, 9, 12, 11, 13];
    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 scores = [12, 12, 1, 4, 1];
    let total = 0;
    for (let i = 0; i < scores.length; i++) {
      total = total + scores[i];
    }
    show(total);
  26. 26.🔁 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
  27. 27.🔁 What is the LAST line this code shows?
    let names = ["Riya", "Meera", "Arjun"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Riya (b) Hi, Meera (c) Hi, Arjun
  28. 28.🔁 What is the LAST line this code shows?
    let names = ["Zoya", "Riya", "Aman", "Arjun", "Meera"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Riya (b) Hi, Zoya (c) Hi, Aman (d) Hi, Meera
  29. 29.🔁 What number does this code show?
    let runs = [17, 4, 2, 17, 4];
    let best = 0;
    for (let i = 0; i < runs.length; i++) {
      if (runs[i] > best) {
        best = runs[i];
      }
    }
    show(best);
  30. 30.🔁 What number does this code show?
    let marks = [15, 9, 3, 14, 11];
    let total = 0;
    for (let i = 0; i < marks.length; i++) {
      total = total + marks[i];
    }
    show(total);
  31. 31.🔁 In for (let i = 0; i < 3; i++), which values does i take? (a) 0, 1, 2, 3 (b) 3 (c) 0, 1, 2 (d) 1, 2, 3
  32. 32.🔁 What number does this code show?
    let steps = [12, 9, 4, 13];
    let total = 0;
    for (let i = 0; i < steps.length; i++) {
      total = total + steps[i];
    }
    show(total);
  33. 33.🔁 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, Meera (c) Hi, Riya
  34. 34.🔁 What is the LAST line this code shows?
    let names = ["Aman", "Meera", "Zoya"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Zoya (b) Hi, Meera (c) Hi, Aman
  35. 35.🔁 What number does this code show?
    let marks = [11, 14, 18, 18];
    let total = 0;
    for (let i = 0; i < marks.length; i++) {
      total = total + marks[i];
    }
    show(total);
  36. 36.🔁 What number does this code show?
    let runs = [98, 52, 28, 19];
    let count = 0;
    for (let i = 0; i < runs.length; i++) {
      if (runs[i] >= 50) {
        count = count + 1;
      }
    }
    show(count);
  37. 37.🔁 What number does this code show?
    let runs = [13, 9, 5];
    let best = 0;
    for (let i = 0; i < runs.length; i++) {
      if (runs[i] > best) {
        best = runs[i];
      }
    }
    show(best);
  38. 38.🔁 What number does this code show?
    let scores = [4, 10, 1];
    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 scores = [56, 51, 46, 96];
    let count = 0;
    for (let i = 0; i < scores.length; i++) {
      if (scores[i] >= 70) {
        count = count + 1;
      }
    }
    show(count);
  40. 40.🔁 What number does this code show?
    let scores = [2, 7, 13, 13];
    let total = 0;
    for (let i = 0; i < scores.length; i++) {
      total = total + scores[i];
    }
    show(total);

Answer key

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

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