← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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