← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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