← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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