← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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