← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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