← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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