← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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