← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

App development lesson 19: Loops over lists · Set 60 · 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) draws icons (c) repeats code (d) saves photos
  2. 2.🔁 What is the LAST line this code shows?
    let names = ["Kabir", "Arjun", "Aman", "Meera", "Zoya"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Kabir (b) Hi, Arjun (c) Hi, Aman (d) Hi, Zoya
  3. 3.🔁 What is the LAST line this code shows?
    let names = ["Kabir", "Zoya", "Meera", "Arjun", "Aman"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Aman (b) Hi, Meera (c) Hi, Zoya (d) Hi, Kabir
  4. 4.🔁 list.length tells you ___. (a) the last item (b) how many items it has (c) the biggest item (d) the first item
  5. 5.🔁 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
  6. 6.🔁 What number does this code show?
    let steps = [14, 25, 83, 44, 27];
    let count = 0;
    for (let i = 0; i < steps.length; i++) {
      if (steps[i] >= 40) {
        count = count + 1;
      }
    }
    show(count);
  7. 7.🔁 What is the LAST line this code shows?
    let names = ["Riya", "Aman", "Zoya"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Riya (b) Hi, Zoya (c) Hi, Aman
  8. 8.🔁 What number does this code show?
    let marks = [10, 4, 18];
    let best = 0;
    for (let i = 0; i < marks.length; i++) {
      if (marks[i] > best) {
        best = marks[i];
      }
    }
    show(best);
  9. 9.🔁 What number does this code show?
    let scores = [6, 7, 14];
    let total = 0;
    for (let i = 0; i < scores.length; i++) {
      total = total + scores[i];
    }
    show(total);
  10. 10.🔁 What number does this code show?
    let marks = [37, 71, 54, 99, 20];
    let count = 0;
    for (let i = 0; i < marks.length; i++) {
      if (marks[i] >= 40) {
        count = count + 1;
      }
    }
    show(count);
  11. 11.🔁 What is the LAST line this code shows?
    let names = ["Zoya", "Kabir", "Riya", "Arjun", "Meera"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Meera (b) Hi, Kabir (c) Hi, Riya (d) Hi, Zoya
  12. 12.🔁 What number does this code show?
    let scores = [5, 8, 20, 11, 9];
    let best = 0;
    for (let i = 0; i < scores.length; i++) {
      if (scores[i] > best) {
        best = scores[i];
      }
    }
    show(best);
  13. 13.🔁 A loop over a list of 5 items runs its body how many times? (a) 1 (b) 6 (c) 4 (d) 5
  14. 14.🔁 What number does this code show?
    let scores = [18, 9, 4, 18];
    let best = 0;
    for (let i = 0; i < scores.length; i++) {
      if (scores[i] > best) {
        best = scores[i];
      }
    }
    show(best);
  15. 15.🔁 What number does this code show?
    let scores = [7, 18, 14, 13, 2];
    let total = 0;
    for (let i = 0; i < scores.length; i++) {
      total = total + scores[i];
    }
    show(total);
  16. 16.🔁 What is the LAST line this code shows?
    let names = ["Aman", "Kabir", "Zoya"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Aman (b) Hi, Kabir (c) Hi, Zoya
  17. 17.🔁 What is the LAST line this code shows?
    let names = ["Aman", "Kabir", "Arjun", "Riya"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Aman (b) Hi, Kabir (c) Hi, Riya (d) Hi, Arjun
  18. 18.🔁 What is the LAST line this code shows?
    let names = ["Kabir", "Meera", "Aman", "Riya"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Aman (b) Hi, Meera (c) Hi, Riya (d) Hi, Kabir
  19. 19.🔁 In for (let i = 0; i < 3; i++), which values does i take? (a) 1, 2, 3 (b) 0, 1, 2 (c) 3 (d) 0, 1, 2, 3
  20. 20.🔁 What number does this code show?
    let marks = [12, 10, 2, 15];
    let total = 0;
    for (let i = 0; i < marks.length; i++) {
      total = total + marks[i];
    }
    show(total);
  21. 21.🔁 What is the LAST line this code shows?
    let names = ["Meera", "Aman", "Riya", "Zoya"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Riya (b) Hi, Aman (c) Hi, Meera (d) Hi, Zoya
  22. 22.🔁 What is the LAST line this code shows?
    let names = ["Riya", "Zoya", "Aman", "Kabir", "Arjun"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Zoya (b) Hi, Aman (c) Hi, Arjun (d) Hi, Riya
  23. 23.🔁 What number does this code show?
    let scores = [5, 6, 5];
    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 scores = [2, 1, 1];
    let best = 0;
    for (let i = 0; i < scores.length; i++) {
      if (scores[i] > best) {
        best = scores[i];
      }
    }
    show(best);
  25. 25.🔁 What number does this code show?
    let scores = [15, 46, 85, 23, 71];
    let count = 0;
    for (let i = 0; i < scores.length; i++) {
      if (scores[i] >= 50) {
        count = count + 1;
      }
    }
    show(count);
  26. 26.🔁 What number does this code show?
    let marks = [85, 22, 75, 68];
    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 marks = [37, 85, 41, 95, 37];
    let count = 0;
    for (let i = 0; i < marks.length; i++) {
      if (marks[i] >= 40) {
        count = count + 1;
      }
    }
    show(count);
  28. 28.🔁 What is the LAST line this code shows?
    let names = ["Riya", "Zoya", "Aman", "Meera", "Arjun"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Riya (b) Hi, Arjun (c) Hi, Zoya (d) Hi, Aman
  29. 29.🔁 What number does this code show?
    let runs = [31, 20, 82, 57];
    let count = 0;
    for (let i = 0; i < runs.length; i++) {
      if (runs[i] >= 60) {
        count = count + 1;
      }
    }
    show(count);
  30. 30.🔁 What number does this code show?
    let marks = [69, 42, 22, 32, 68];
    let count = 0;
    for (let i = 0; i < marks.length; i++) {
      if (marks[i] >= 50) {
        count = count + 1;
      }
    }
    show(count);
  31. 31.🔁 What number does this code show?
    let scores = [83, 45, 23];
    let count = 0;
    for (let i = 0; i < scores.length; i++) {
      if (scores[i] >= 50) {
        count = count + 1;
      }
    }
    show(count);
  32. 32.🔁 What number does this code show?
    let scores = [83, 94, 94, 86];
    let count = 0;
    for (let i = 0; i < scores.length; i++) {
      if (scores[i] >= 50) {
        count = count + 1;
      }
    }
    show(count);
  33. 33.🔁 What number does this code show?
    let steps = [31, 25, 49, 76, 38];
    let count = 0;
    for (let i = 0; i < steps.length; i++) {
      if (steps[i] >= 40) {
        count = count + 1;
      }
    }
    show(count);
  34. 34.🔁 What is the LAST line this code shows?
    let names = ["Kabir", "Arjun", "Zoya", "Meera"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Kabir (b) Hi, Zoya (c) Hi, Arjun (d) Hi, Meera
  35. 35.🔁 What is the LAST line this code shows?
    let names = ["Arjun", "Kabir", "Zoya", "Riya"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Arjun (b) Hi, Kabir (c) Hi, Zoya (d) Hi, Riya
  36. 36.🔁 What number does this code show?
    let scores = [68, 35, 90];
    let count = 0;
    for (let i = 0; i < scores.length; i++) {
      if (scores[i] >= 60) {
        count = count + 1;
      }
    }
    show(count);
  37. 37.🔁 What number does this code show?
    let steps = [1, 5, 8, 20, 20];
    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 scores = [50, 21, 93];
    let count = 0;
    for (let i = 0; i < scores.length; i++) {
      if (scores[i] >= 50) {
        count = count + 1;
      }
    }
    show(count);
  39. 39.🔁 What number does this code show?
    let marks = [54, 38, 12];
    let count = 0;
    for (let i = 0; i < marks.length; i++) {
      if (marks[i] >= 50) {
        count = count + 1;
      }
    }
    show(count);
  40. 40.🔁 What is the LAST line this code shows?
    let names = ["Riya", "Aman", "Kabir", "Arjun", "Zoya"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Kabir (b) Hi, Riya (c) Hi, Aman (d) Hi, Zoya

Answer key

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

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