← Loops over lists lesson New set →

App Dev Worksheet: Loops over Lists

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

Answer key

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

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