← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

App development lesson 19: Loops over lists · Set 503 · 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.🔁 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
  2. 2.🔁 What is the LAST line this code shows?
    let names = ["Riya", "Arjun", "Aman", "Zoya"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Arjun (b) Hi, Aman (c) Hi, Riya (d) Hi, Zoya
  3. 3.🔁 list.length tells you ___. (a) how many items it has (b) the first item (c) the biggest item (d) the last item
  4. 4.🔁 What is the LAST line this code shows?
    let names = ["Zoya", "Meera", "Riya", "Arjun", "Kabir"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Zoya (b) Hi, Meera (c) Hi, Kabir (d) Hi, Riya
  5. 5.🔁 What is the LAST line this code shows?
    let names = ["Meera", "Kabir", "Aman", "Riya", "Arjun"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Meera (b) Hi, Kabir (c) Hi, Arjun (d) Hi, Aman
  6. 6.🔁 What is the LAST line this code shows?
    let names = ["Arjun", "Meera", "Aman"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Arjun (b) Hi, Meera (c) Hi, Aman
  7. 7.🔁 What number does this code show?
    let steps = [14, 63, 17, 67];
    let count = 0;
    for (let i = 0; i < steps.length; i++) {
      if (steps[i] >= 60) {
        count = count + 1;
      }
    }
    show(count);
  8. 8.🔁 A loop over a list of 5 items runs its body how many times? (a) 5 (b) 1 (c) 4 (d) 6
  9. 9.🔁 What is the LAST line this code shows?
    let names = ["Kabir", "Riya", "Zoya", "Arjun", "Aman"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Zoya (b) Hi, Kabir (c) Hi, Riya (d) Hi, Aman
  10. 10.🔁 What is the LAST line this code shows?
    let names = ["Meera", "Zoya", "Aman", "Riya"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Riya (b) Hi, Zoya (c) Hi, Aman (d) Hi, Meera
  11. 11.🔁 What number does this code show?
    let scores = [98, 45, 22, 62];
    let count = 0;
    for (let i = 0; i < scores.length; i++) {
      if (scores[i] >= 60) {
        count = count + 1;
      }
    }
    show(count);
  12. 12.🔁 What number does this code show?
    let marks = [3, 12, 13, 18, 7];
    let total = 0;
    for (let i = 0; i < marks.length; i++) {
      total = total + marks[i];
    }
    show(total);
  13. 13.🔁 What is the LAST line this code shows?
    let names = ["Zoya", "Kabir", "Arjun", "Aman"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Arjun (b) Hi, Zoya (c) Hi, Kabir (d) Hi, Aman
  14. 14.🔁 What does a loop do? (a) saves photos (b) draws icons (c) repeats code (d) deletes code
  15. 15.🔁 What number does this code show?
    let runs = [9, 16, 15];
    let best = 0;
    for (let i = 0; i < runs.length; i++) {
      if (runs[i] > best) {
        best = runs[i];
      }
    }
    show(best);
  16. 16.🔁 What number does this code show?
    let runs = [14, 13, 13, 1, 8];
    let total = 0;
    for (let i = 0; i < runs.length; i++) {
      total = total + runs[i];
    }
    show(total);
  17. 17.🔁 What is the LAST line this code shows?
    let names = ["Meera", "Zoya", "Riya", "Arjun", "Aman"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Aman (b) Hi, Riya (c) Hi, Zoya (d) Hi, Meera
  18. 18.🔁 What number does this code show?
    let scores = [11, 16, 11];
    let best = 0;
    for (let i = 0; i < scores.length; i++) {
      if (scores[i] > best) {
        best = scores[i];
      }
    }
    show(best);
  19. 19.🔁 What number does this code show?
    let marks = [37, 12, 70, 13, 36];
    let count = 0;
    for (let i = 0; i < marks.length; i++) {
      if (marks[i] >= 60) {
        count = count + 1;
      }
    }
    show(count);
  20. 20.🔁 In for (let i = 0; i < 3; i++), which values does i take? (a) 1, 2, 3 (b) 0, 1, 2 (c) 0, 1, 2, 3 (d) 3
  21. 21.🔁 What number does this code show?
    let steps = [12, 68, 27];
    let count = 0;
    for (let i = 0; i < steps.length; i++) {
      if (steps[i] >= 60) {
        count = count + 1;
      }
    }
    show(count);
  22. 22.🔁 What number does this code show?
    let marks = [16, 16, 14, 14];
    let best = 0;
    for (let i = 0; i < marks.length; i++) {
      if (marks[i] > best) {
        best = marks[i];
      }
    }
    show(best);
  23. 23.🔁 What number does this code show?
    let marks = [9, 16, 7];
    let best = 0;
    for (let i = 0; i < marks.length; i++) {
      if (marks[i] > best) {
        best = marks[i];
      }
    }
    show(best);
  24. 24.🔁 What number does this code show?
    let scores = [6, 4, 19];
    let best = 0;
    for (let i = 0; i < scores.length; i++) {
      if (scores[i] > best) {
        best = scores[i];
      }
    }
    show(best);
  25. 25.🔁 What is the LAST line this code shows?
    let names = ["Aman", "Zoya", "Kabir", "Arjun"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Aman (b) Hi, Kabir (c) Hi, Zoya (d) Hi, Arjun
  26. 26.🔁 What is the LAST line this code shows?
    let names = ["Zoya", "Aman", "Arjun", "Meera"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Arjun (b) Hi, Meera (c) Hi, Aman (d) Hi, Zoya
  27. 27.🔁 What number does this code show?
    let marks = [80, 76, 29, 68, 79];
    let count = 0;
    for (let i = 0; i < marks.length; i++) {
      if (marks[i] >= 40) {
        count = count + 1;
      }
    }
    show(count);
  28. 28.🔁 What number does this code show?
    let marks = [35, 57, 87, 24, 38];
    let count = 0;
    for (let i = 0; i < marks.length; i++) {
      if (marks[i] >= 60) {
        count = count + 1;
      }
    }
    show(count);
  29. 29.🔁 What number does this code show?
    let runs = [16, 10, 17, 8];
    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 runs = [5, 13, 11, 5];
    let total = 0;
    for (let i = 0; i < runs.length; i++) {
      total = total + runs[i];
    }
    show(total);
  31. 31.🔁 What number does this code show?
    let scores = [14, 14, 9];
    let total = 0;
    for (let i = 0; i < scores.length; i++) {
      total = total + scores[i];
    }
    show(total);
  32. 32.🔁 What is the LAST line this code shows?
    let names = ["Arjun", "Riya", "Meera", "Aman", "Zoya"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Arjun (b) Hi, Meera (c) Hi, Riya (d) Hi, Zoya
  33. 33.🔁 What number does this code show?
    let scores = [20, 12, 66];
    let count = 0;
    for (let i = 0; i < scores.length; i++) {
      if (scores[i] >= 60) {
        count = count + 1;
      }
    }
    show(count);
  34. 34.🔁 What number does this code show?
    let steps = [19, 40, 87];
    let count = 0;
    for (let i = 0; i < steps.length; i++) {
      if (steps[i] >= 50) {
        count = count + 1;
      }
    }
    show(count);
  35. 35.🔁 What number does this code show?
    let scores = [8, 10, 11];
    let total = 0;
    for (let i = 0; i < scores.length; i++) {
      total = total + scores[i];
    }
    show(total);
  36. 36.🔁 What number does this code show?
    let runs = [10, 6, 13, 16, 15];
    let total = 0;
    for (let i = 0; i < runs.length; i++) {
      total = total + runs[i];
    }
    show(total);
  37. 37.🔁 What is the LAST line this code shows?
    let names = ["Meera", "Arjun", "Zoya", "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
  38. 38.🔁 What is the LAST line this code shows?
    let names = ["Kabir", "Zoya", "Arjun", "Aman", "Riya"];
    for (let i = 0; i < names.length; i++) {
      show("Hi, " + names[i]);
    }
    (a) Hi, Kabir (b) Hi, Arjun (c) Hi, Zoya (d) Hi, Riya
  39. 39.🔁 What number does this code show?
    let scores = [13, 10, 17];
    let total = 0;
    for (let i = 0; i < scores.length; i++) {
      total = total + scores[i];
    }
    show(total);
  40. 40.🔁 What number does this code show?
    let steps = [54, 65, 37, 56, 32];
    let count = 0;
    for (let i = 0; i < steps.length; i++) {
      if (steps[i] >= 40) {
        count = count + 1;
      }
    }
    show(count);

Answer key

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

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