← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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