← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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