← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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