← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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