← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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