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're about to loop through a list to add up all its numbers. What should you set the running total variable to before the loop starts?

    • 0
    • 1
    • The first number in the list
    • The last number in the list
  2. A program loops through the list [4, 7, 2, 9] 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 [-3, 5, -1, 8, -9, 2] are negative, starting the counter at 0. What is the counter after the loop finishes?

    • 2
    • 3
    • 4
    • 5
  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 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 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 · Starting at 0 means the total isn't changed until you actually add the first real number from the list.
  2. 22 · 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. 3 · Only -3, -1, and -9 pass the negative test, so the counter increases three 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. 55 · Adding each number in order builds the total step by step until every item has been included.
Worksheet · LightMySky