← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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