← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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