← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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