← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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