← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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