← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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