← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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