← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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