← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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