← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

App development lesson 19: Loops over lists · Set 990 · 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 runs = [42, 26, 45];
    let count = 0;
    for (let i = 0; i < runs.length; i++) {
      if (runs[i] >= 50) {
        count = count + 1;
      }
    }
    show(count);
  2. 2.🔁 A loop over a list of 5 items runs its body how many times? (a) 6 (b) 1 (c) 4 (d) 5
  3. 3.🔁 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
  4. 4.🔁 What number does this code show?
    let runs = [12, 6, 9, 12, 3];
    let total = 0;
    for (let i = 0; i < runs.length; i++) {
      total = total + runs[i];
    }
    show(total);
  5. 5.🔁 What number does this code show?
    let marks = [14, 6, 5];
    let best = 0;
    for (let i = 0; i < marks.length; i++) {
      if (marks[i] > best) {
        best = marks[i];
      }
    }
    show(best);
  6. 6.🔁 What is the LAST line this code shows?
    let names = ["Meera", "Kabir", "Riya", "Aman"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Aman (b) Hi, Kabir (c) Hi, Riya (d) Hi, Meera
  7. 7.🔁 What is the LAST line this code shows?
    let names = ["Arjun", "Zoya", "Aman"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Aman (b) Hi, Arjun (c) Hi, Zoya
  8. 8.🔁 What number does this code show?
    let steps = [6, 2, 7];
    let total = 0;
    for (let i = 0; i < steps.length; i++) {
      total = total + steps[i];
    }
    show(total);
  9. 9.🔁 What number does this code show?
    let scores = [16, 9, 15, 5, 10];
    let best = 0;
    for (let i = 0; i < scores.length; i++) {
      if (scores[i] > best) {
        best = scores[i];
      }
    }
    show(best);
  10. 10.🔁 What number does this code show?
    let scores = [13, 9, 2];
    let total = 0;
    for (let i = 0; i < scores.length; i++) {
      total = total + scores[i];
    }
    show(total);
  11. 11.🔁 What number does this code show?
    let marks = [5, 16, 3];
    let total = 0;
    for (let i = 0; i < marks.length; i++) {
      total = total + marks[i];
    }
    show(total);
  12. 12.🔁 What does a loop do? (a) repeats code (b) draws icons (c) saves photos (d) deletes code
  13. 13.🔁 What number does this code show?
    let scores = [15, 98, 28, 88, 16];
    let count = 0;
    for (let i = 0; i < scores.length; i++) {
      if (scores[i] >= 70) {
        count = count + 1;
      }
    }
    show(count);
  14. 14.🔁 What number does this code show?
    let scores = [10, 12, 16, 16];
    let best = 0;
    for (let i = 0; i < scores.length; i++) {
      if (scores[i] > best) {
        best = scores[i];
      }
    }
    show(best);
  15. 15.🔁 What number does this code show?
    let runs = [7, 3, 5];
    let best = 0;
    for (let i = 0; i < runs.length; i++) {
      if (runs[i] > best) {
        best = runs[i];
      }
    }
    show(best);
  16. 16.🔁 What is the LAST line this code shows?
    let names = ["Arjun", "Zoya", "Riya"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Arjun (b) Hi, Zoya (c) Hi, Riya
  17. 17.🔁 What number does this code show?
    let marks = [15, 13, 18];
    let best = 0;
    for (let i = 0; i < marks.length; i++) {
      if (marks[i] > best) {
        best = marks[i];
      }
    }
    show(best);
  18. 18.🔁 What number does this code show?
    let runs = [49, 28, 60];
    let count = 0;
    for (let i = 0; i < runs.length; i++) {
      if (runs[i] >= 50) {
        count = count + 1;
      }
    }
    show(count);
  19. 19.🔁 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
  20. 20.🔁 What number does this code show?
    let scores = [7, 14, 16, 12];
    let best = 0;
    for (let i = 0; i < scores.length; i++) {
      if (scores[i] > best) {
        best = scores[i];
      }
    }
    show(best);
  21. 21.🔁 What is the LAST line this code shows?
    let names = ["Arjun", "Aman", "Riya"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Aman (b) Hi, Riya (c) Hi, Arjun
  22. 22.🔁 What number does this code show?
    let marks = [13, 20, 17, 13];
    let total = 0;
    for (let i = 0; i < marks.length; i++) {
      total = total + marks[i];
    }
    show(total);
  23. 23.🔁 What number does this code show?
    let runs = [42, 44, 60, 79];
    let count = 0;
    for (let i = 0; i < runs.length; i++) {
      if (runs[i] >= 60) {
        count = count + 1;
      }
    }
    show(count);
  24. 24.🔁 What number does this code show?
    let scores = [14, 14, 20, 20, 4];
    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 = ["Riya", "Meera", "Zoya", "Aman", "Kabir"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Meera (b) Hi, Zoya (c) Hi, Riya (d) Hi, Kabir
  26. 26.🔁 What number does this code show?
    let runs = [18, 18, 15, 13, 19];
    let best = 0;
    for (let i = 0; i < runs.length; i++) {
      if (runs[i] > best) {
        best = runs[i];
      }
    }
    show(best);
  27. 27.🔁 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, Riya (b) Hi, Aman (c) Hi, Meera (d) Hi, Kabir
  28. 28.🔁 What number does this code show?
    let marks = [25, 26, 57, 84];
    let count = 0;
    for (let i = 0; i < marks.length; i++) {
      if (marks[i] >= 70) {
        count = count + 1;
      }
    }
    show(count);
  29. 29.🔁 What number does this code show?
    let scores = [9, 10, 17];
    let total = 0;
    for (let i = 0; i < scores.length; i++) {
      total = total + scores[i];
    }
    show(total);
  30. 30.🔁 What is the LAST line this code shows?
    let names = ["Riya", "Meera", "Zoya", "Aman"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Aman (b) Hi, Riya (c) Hi, Zoya (d) Hi, Meera
  31. 31.🔁 What number does this code show?
    let runs = [13, 16, 20];
    let best = 0;
    for (let i = 0; i < runs.length; i++) {
      if (runs[i] > best) {
        best = runs[i];
      }
    }
    show(best);
  32. 32.🔁 What number does this code show?
    let steps = [11, 4, 1];
    let total = 0;
    for (let i = 0; i < steps.length; i++) {
      total = total + steps[i];
    }
    show(total);
  33. 33.🔁 What number does this code show?
    let steps = [1, 14, 3, 16, 15];
    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 steps = [19, 17, 6];
    let best = 0;
    for (let i = 0; i < steps.length; i++) {
      if (steps[i] > best) {
        best = steps[i];
      }
    }
    show(best);
  35. 35.🔁 What is the LAST line this code shows?
    let names = ["Arjun", "Kabir", "Zoya", "Aman"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Arjun (b) Hi, Kabir (c) Hi, Aman (d) Hi, Zoya
  36. 36.🔁 What number does this code show?
    let steps = [8, 2, 19];
    let total = 0;
    for (let i = 0; i < steps.length; i++) {
      total = total + steps[i];
    }
    show(total);
  37. 37.🔁 What number does this code show?
    let scores = [6, 7, 7, 20, 11];
    let total = 0;
    for (let i = 0; i < scores.length; i++) {
      total = total + scores[i];
    }
    show(total);
  38. 38.🔁 What is the LAST line this code shows?
    let names = ["Riya", "Aman", "Zoya", "Arjun", "Kabir"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Zoya (b) Hi, Kabir (c) Hi, Riya (d) Hi, Aman
  39. 39.🔁 What number does this code show?
    let steps = [17, 12, 10];
    let total = 0;
    for (let i = 0; i < steps.length; i++) {
      total = total + steps[i];
    }
    show(total);
  40. 40.🔁 What number does this code show?
    let scores = [11, 15, 2];
    let total = 0;
    for (let i = 0; i < scores.length; i++) {
      total = total + scores[i];
    }
    show(total);

Answer key

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

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