← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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