← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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