← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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