← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

App development lesson 19: Loops over lists · Set 246 · 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 = [15, 7, 1, 12];
    let best = 0;
    for (let i = 0; i < scores.length; i++) {
      if (scores[i] > best) {
        best = scores[i];
      }
    }
    show(best);
  2. 2.🔁 What number does this code show?
    let scores = [36, 54, 77, 58];
    let count = 0;
    for (let i = 0; i < scores.length; i++) {
      if (scores[i] >= 70) {
        count = count + 1;
      }
    }
    show(count);
  3. 3.🔁 In for (let i = 0; i < 3; i++), which values does i take? (a) 3 (b) 0, 1, 2 (c) 0, 1, 2, 3 (d) 1, 2, 3
  4. 4.🔁 What is the LAST line this code shows?
    let names = ["Riya", "Meera", "Aman", "Arjun", "Zoya"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Zoya (b) Hi, Riya (c) Hi, Meera (d) Hi, Aman
  5. 5.🔁 What does a loop do? (a) deletes code (b) draws icons (c) repeats code (d) saves photos
  6. 6.🔁 What number does this code show?
    let marks = [68, 36, 62, 10, 97];
    let count = 0;
    for (let i = 0; i < marks.length; i++) {
      if (marks[i] >= 40) {
        count = count + 1;
      }
    }
    show(count);
  7. 7.🔁 A loop over a list of 5 items runs its body how many times? (a) 6 (b) 4 (c) 5 (d) 1
  8. 8.🔁 What number does this code show?
    let marks = [12, 1, 5];
    let best = 0;
    for (let i = 0; i < marks.length; i++) {
      if (marks[i] > best) {
        best = marks[i];
      }
    }
    show(best);
  9. 9.🔁 What number does this code show?
    let scores = [5, 14, 17, 20];
    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 steps = [12, 11, 8];
    let best = 0;
    for (let i = 0; i < steps.length; i++) {
      if (steps[i] > best) {
        best = steps[i];
      }
    }
    show(best);
  11. 11.🔁 What number does this code show?
    let runs = [10, 2, 8, 4];
    let best = 0;
    for (let i = 0; i < runs.length; i++) {
      if (runs[i] > best) {
        best = runs[i];
      }
    }
    show(best);
  12. 12.🔁 list.length tells you ___. (a) the biggest item (b) the first item (c) how many items it has (d) the last item
  13. 13.🔁 What is the LAST line this code shows?
    let names = ["Arjun", "Aman", "Riya", "Kabir", "Zoya"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Riya (b) Hi, Arjun (c) Hi, Aman (d) Hi, Zoya
  14. 14.🔁 What number does this code show?
    let runs = [1, 15, 6, 19, 5];
    let total = 0;
    for (let i = 0; i < runs.length; i++) {
      total = total + runs[i];
    }
    show(total);
  15. 15.🔁 What is the LAST line this code shows?
    let names = ["Riya", "Zoya", "Kabir", "Arjun", "Aman"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Kabir (b) Hi, Riya (c) Hi, Zoya (d) Hi, Aman
  16. 16.🔁 What number does this code show?
    let steps = [1, 6, 20];
    let total = 0;
    for (let i = 0; i < steps.length; i++) {
      total = total + steps[i];
    }
    show(total);
  17. 17.🔁 What is the LAST line this code shows?
    let names = ["Arjun", "Riya", "Kabir", "Zoya"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Arjun (b) Hi, Kabir (c) Hi, Zoya (d) Hi, Riya
  18. 18.🔁 What number does this code show?
    let marks = [68, 46, 95];
    let count = 0;
    for (let i = 0; i < marks.length; i++) {
      if (marks[i] >= 70) {
        count = count + 1;
      }
    }
    show(count);
  19. 19.🔁 What number does this code show?
    let steps = [2, 3, 19, 9, 6];
    let best = 0;
    for (let i = 0; i < steps.length; i++) {
      if (steps[i] > best) {
        best = steps[i];
      }
    }
    show(best);
  20. 20.🔁 What number does this code show?
    let steps = [19, 19, 14, 11, 14];
    let best = 0;
    for (let i = 0; i < steps.length; i++) {
      if (steps[i] > best) {
        best = steps[i];
      }
    }
    show(best);
  21. 21.🔁 What number does this code show?
    let steps = [52, 84, 22, 68, 66];
    let count = 0;
    for (let i = 0; i < steps.length; i++) {
      if (steps[i] >= 40) {
        count = count + 1;
      }
    }
    show(count);
  22. 22.🔁 What is the LAST line this code shows?
    let names = ["Aman", "Kabir", "Arjun", "Zoya", "Riya"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Kabir (b) Hi, Arjun (c) Hi, Riya (d) Hi, Aman
  23. 23.🔁 What is the LAST line this code shows?
    let names = ["Riya", "Arjun", "Kabir"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Kabir (b) Hi, Riya (c) Hi, Arjun
  24. 24.🔁 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, Aman (b) Hi, Arjun (c) Hi, Meera
  25. 25.🔁 What number does this code show?
    let runs = [18, 41, 70];
    let count = 0;
    for (let i = 0; i < runs.length; i++) {
      if (runs[i] >= 40) {
        count = count + 1;
      }
    }
    show(count);
  26. 26.🔁 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, Arjun (b) Hi, Zoya (c) Hi, Meera
  27. 27.🔁 What number does this code show?
    let runs = [18, 12, 16];
    let total = 0;
    for (let i = 0; i < runs.length; i++) {
      total = total + runs[i];
    }
    show(total);
  28. 28.🔁 What is the LAST line this code shows?
    let names = ["Zoya", "Arjun", "Meera", "Aman"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Zoya (b) Hi, Arjun (c) Hi, Aman (d) Hi, Meera
  29. 29.🔁 What number does this code show?
    let runs = [11, 1, 16];
    let total = 0;
    for (let i = 0; i < runs.length; i++) {
      total = total + runs[i];
    }
    show(total);
  30. 30.🔁 What is the LAST line this code shows?
    let names = ["Aman", "Riya", "Arjun", "Kabir"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Kabir (b) Hi, Arjun (c) Hi, Aman (d) Hi, Riya
  31. 31.🔁 What is the LAST line this code shows?
    let names = ["Kabir", "Aman", "Riya"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Riya (b) Hi, Kabir (c) Hi, Aman
  32. 32.🔁 What is the LAST line this code shows?
    let names = ["Riya", "Aman", "Kabir"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Kabir (b) Hi, Aman (c) Hi, Riya
  33. 33.🔁 What number does this code show?
    let scores = [10, 12, 9, 19, 4];
    let total = 0;
    for (let i = 0; i < scores.length; i++) {
      total = total + scores[i];
    }
    show(total);
  34. 34.🔁 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
  35. 35.🔁 What is the LAST line this code shows?
    let names = ["Meera", "Kabir", "Aman", "Zoya"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Aman (b) Hi, Kabir (c) Hi, Zoya (d) Hi, Meera
  36. 36.🔁 What is the LAST line this code shows?
    let names = ["Kabir", "Riya", "Meera", "Aman"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Kabir (b) Hi, Meera (c) Hi, Aman (d) Hi, Riya
  37. 37.🔁 What number does this code show?
    let steps = [69, 85, 95, 13, 68];
    let count = 0;
    for (let i = 0; i < steps.length; i++) {
      if (steps[i] >= 40) {
        count = count + 1;
      }
    }
    show(count);
  38. 38.🔁 What number does this code show?
    let runs = [19, 12, 12, 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 steps = [5, 18, 4, 17];
    let total = 0;
    for (let i = 0; i < steps.length; i++) {
      total = total + steps[i];
    }
    show(total);
  40. 40.🔁 What number does this code show?
    let scores = [18, 11, 17];
    let total = 0;
    for (let i = 0; i < scores.length; i++) {
      total = total + scores[i];
    }
    show(total);

Answer key

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

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