← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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