← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

App development lesson 19: Loops over lists · Set 937 · 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 = [19, 17, 11];
    let total = 0;
    for (let i = 0; i < steps.length; i++) {
      total = total + steps[i];
    }
    show(total);
  2. 2.🔁 What is the LAST line this code shows?
    let names = ["Aman", "Arjun", "Riya", "Zoya", "Meera"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Aman (b) Hi, Meera (c) Hi, Arjun (d) Hi, Riya
  3. 3.🔁 What number does this code show?
    let steps = [5, 20, 11, 1];
    let total = 0;
    for (let i = 0; i < steps.length; i++) {
      total = total + steps[i];
    }
    show(total);
  4. 4.🔁 What number does this code show?
    let scores = [2, 20, 4, 7, 12];
    let best = 0;
    for (let i = 0; i < scores.length; i++) {
      if (scores[i] > best) {
        best = scores[i];
      }
    }
    show(best);
  5. 5.🔁 What number does this code show?
    let runs = [6, 18, 12, 3];
    let total = 0;
    for (let i = 0; i < runs.length; i++) {
      total = total + runs[i];
    }
    show(total);
  6. 6.🔁 A loop over a list of 5 items runs its body how many times? (a) 6 (b) 5 (c) 4 (d) 1
  7. 7.🔁 What number does this code show?
    let runs = [5, 6, 18, 1, 13];
    let total = 0;
    for (let i = 0; i < runs.length; i++) {
      total = total + runs[i];
    }
    show(total);
  8. 8.🔁 list.length tells you ___. (a) how many items it has (b) the last item (c) the biggest item (d) the first item
  9. 9.🔁 What number does this code show?
    let steps = [72, 48, 62];
    let count = 0;
    for (let i = 0; i < steps.length; i++) {
      if (steps[i] >= 40) {
        count = count + 1;
      }
    }
    show(count);
  10. 10.🔁 What is the LAST line this code shows?
    let names = ["Aman", "Riya", "Kabir"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Riya (b) Hi, Kabir (c) Hi, Aman
  11. 11.🔁 What number does this code show?
    let scores = [1, 12, 2, 16, 20];
    let total = 0;
    for (let i = 0; i < scores.length; i++) {
      total = total + scores[i];
    }
    show(total);
  12. 12.🔁 What number does this code show?
    let steps = [19, 2, 4];
    let total = 0;
    for (let i = 0; i < steps.length; i++) {
      total = total + steps[i];
    }
    show(total);
  13. 13.🔁 What number does this code show?
    let scores = [29, 22, 80];
    let count = 0;
    for (let i = 0; i < scores.length; i++) {
      if (scores[i] >= 70) {
        count = count + 1;
      }
    }
    show(count);
  14. 14.🔁 What number does this code show?
    let scores = [3, 4, 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 scores = [11, 2, 8];
    let total = 0;
    for (let i = 0; i < scores.length; i++) {
      total = total + scores[i];
    }
    show(total);
  16. 16.🔁 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
  17. 17.🔁 What is the LAST line this code shows?
    let names = ["Meera", "Zoya", "Kabir"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Zoya (b) Hi, Kabir (c) Hi, Meera
  18. 18.🔁 What is the LAST line this code shows?
    let names = ["Arjun", "Aman", "Meera", "Zoya"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Zoya (b) Hi, Meera (c) Hi, Aman (d) Hi, Arjun
  19. 19.🔁 What is the LAST line this code shows?
    let names = ["Zoya", "Aman", "Meera"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Zoya (b) Hi, Aman (c) Hi, Meera
  20. 20.🔁 What number does this code show?
    let scores = [71, 96, 96];
    let count = 0;
    for (let i = 0; i < scores.length; i++) {
      if (scores[i] >= 60) {
        count = count + 1;
      }
    }
    show(count);
  21. 21.🔁 What number does this code show?
    let marks = [7, 17, 16, 3];
    let best = 0;
    for (let i = 0; i < marks.length; i++) {
      if (marks[i] > best) {
        best = marks[i];
      }
    }
    show(best);
  22. 22.🔁 What number does this code show?
    let runs = [70, 95, 79];
    let count = 0;
    for (let i = 0; i < runs.length; i++) {
      if (runs[i] >= 60) {
        count = count + 1;
      }
    }
    show(count);
  23. 23.🔁 What number does this code show?
    let scores = [9, 9, 4, 11];
    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 scores = [2, 1, 6, 7];
    let best = 0;
    for (let i = 0; i < scores.length; i++) {
      if (scores[i] > best) {
        best = scores[i];
      }
    }
    show(best);
  25. 25.🔁 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
  26. 26.🔁 What number does this code show?
    let marks = [11, 16, 11, 15, 6];
    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 = [5, 1, 8, 12];
    let best = 0;
    for (let i = 0; i < scores.length; i++) {
      if (scores[i] > best) {
        best = scores[i];
      }
    }
    show(best);
  28. 28.🔁 What number does this code show?
    let steps = [80, 81, 47];
    let count = 0;
    for (let i = 0; i < steps.length; i++) {
      if (steps[i] >= 50) {
        count = count + 1;
      }
    }
    show(count);
  29. 29.🔁 What number does this code show?
    let runs = [1, 18, 17, 2, 4];
    let total = 0;
    for (let i = 0; i < runs.length; i++) {
      total = total + runs[i];
    }
    show(total);
  30. 30.🔁 What number does this code show?
    let scores = [8, 18, 3];
    let total = 0;
    for (let i = 0; i < scores.length; i++) {
      total = total + scores[i];
    }
    show(total);
  31. 31.🔁 What number does this code show?
    let scores = [7, 3, 19, 11, 1];
    let best = 0;
    for (let i = 0; i < scores.length; i++) {
      if (scores[i] > best) {
        best = scores[i];
      }
    }
    show(best);
  32. 32.🔁 What number does this code show?
    let scores = [62, 59, 73, 31];
    let count = 0;
    for (let i = 0; i < scores.length; i++) {
      if (scores[i] >= 70) {
        count = count + 1;
      }
    }
    show(count);
  33. 33.🔁 What number does this code show?
    let scores = [3, 2, 2];
    let total = 0;
    for (let i = 0; i < scores.length; i++) {
      total = total + scores[i];
    }
    show(total);
  34. 34.🔁 What number does this code show?
    let scores = [14, 2, 2];
    let total = 0;
    for (let i = 0; i < scores.length; i++) {
      total = total + scores[i];
    }
    show(total);
  35. 35.🔁 What number does this code show?
    let steps = [11, 9, 9];
    let total = 0;
    for (let i = 0; i < steps.length; i++) {
      total = total + steps[i];
    }
    show(total);
  36. 36.🔁 What is the LAST line this code shows?
    let names = ["Arjun", "Riya", "Zoya", "Meera"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Meera (b) Hi, Arjun (c) Hi, Riya (d) Hi, Zoya
  37. 37.🔁 What is the LAST line this code shows?
    let names = ["Zoya", "Riya", "Meera"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Zoya (b) Hi, Riya (c) Hi, Meera
  38. 38.🔁 What number does this code show?
    let scores = [5, 1, 1, 11];
    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 steps = [10, 16, 3];
    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 marks = [44, 32, 80];
    let count = 0;
    for (let i = 0; i < marks.length; i++) {
      if (marks[i] >= 40) {
        count = count + 1;
      }
    }
    show(count);

Answer key

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

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