Traversing a List to Build a Result · seed 1 · A4, ink-friendly. The answer key prints on its own page for grown-ups.
به انگلیسی

One pass, one running answer

Computing · Programming · ages 15-16
Name ______________________   Date ____________
  1. You are about to loop through a list and count the items that pass a test. What should the counter start at before the loop?

    • 0
    • 1
    • The first item in the list
    • The number of items in the list
  2. A program loops through the list [5, 9, 3, 11] and keeps a running total, starting at 0. What is the total after the loop finishes?

    Answer: ______________

  3. Before totalling a list, what should the running total start at?

    • 0
    • 1
    • The last item
  4. A counting loop adds 1 only when an item passes the test.

    Circle one:   True   False

  5. A loop counts how many items in the list [4, -7, 0, -2, 6, -5, -1] are negative, starting the counter at 0. What is the counter after the loop finishes?

    • 3
    • 4
    • 5
    • 6
  6. In the list [3, 8, 15, 22, 6, 19], how many numbers are greater than 10?

    Answer: ______________

  7. A loop adds up every number in the list [15, 3, 8, 12, 5], starting the running total at 0. What is the final total?

    Answer: ______________

  8. Find the largest of 12, 45, 7, 39, 45 and 3 by checking each once.

    Answer: ______________

  9. A friend starts best-so-far at 0 for -8, -2, -15 and -4. What will the loop claim?

    • -2, the true largest
    • -15, the smallest
    • 0, which sits in no slot
  10. A loop sums the numbers in the list [2, 4, 6, 8, 10, 12], starting the running total at 0. What is the final total?

    Answer: ______________

LightMySky · lightmysky.comW1-mt_lusbjHFt95-s1

Answer key

For grown-ups. Fold this page away before handing over the rest.

One pass, one running answer W1-mt_lusbjHFt95-s1

  1. 0 · Before any item is checked, nothing has passed yet, so the counter starts at 0 and adds 1 per pass.
  2. 28 · Adding each number to the total one at a time gives you the full sum by the end of the loop.
  3. 0 · Starting at 0 leaves the total unchanged until the first real item joins.
  4. True · The counter moves for passes and stays put for the rest.
  5. 4 · Only -7, -2, -5 and -1 pass the negative test. 0 is not negative, so the counter increases four times.
  6. 3 · Checking each number against the rule one at a time tells you how many pass.
  7. 43 · Each number gets folded into the total in order, so the last value you see is the full sum.
  8. 45 · Carrying the best seen so far ends the walk at 45.
  9. 0, which sits in no slot · Every item falls below 0, so nothing ever replaces the start value.
  10. 42 · Adding each number in order builds the total step by step until every item has been included.
Worksheet · LightMySky