← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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