← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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