← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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