← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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