← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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