← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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