← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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