← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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