← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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