← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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