← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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