← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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