← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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