Stacks: Last In, First Out · seed 1 · A4, ink-friendly. The answer key prints on its own page for grown-ups.

The pile you only touch on top

Computing · Algorithms & Data Structures · ages 16-18
Name ______________________   Date ____________
  1. You push 4, push 7, pop, then push 9. What sits on top?

    • 4
    • 7
    • 9
  2. What is the stack rule?

    • First in, first out
    • Last in, first out: the top is the only value within reach
    • Any item can leave at any time
  3. In Python, how do you peek at the top of a stack list without removing it?

    • Index minus one
    • Pop twice
    • Delete the list
  4. Why does an editor undo stack pop the newest change first?

    • That is exactly last in, first out applied to history
    • Oldest work matters least
    • Stacks cannot hold old changes
  5. What should popping an empty stack do?

    • Silently return garbage
    • Signal an error or refuse clearly, as decided upfront
    • Return the oldest value
  6. In bracket matching, leftover opening brackets on the stack mean the nesting is fine.

    Circle one:   True   False

  7. A student uses a stack for a print queue and wonders why jobs print newest first. What is the error?

    • Stacks reverse arrival order, so a queue is the right tool for arrival order
    • Stacks cannot hold documents
    • Printers cannot use any data structure
  8. Push A, push B, pop, push C, pop, pop. What order do values come off?

    • A, then B, then C
    • C, then B, then A
    • B, then C, then A
LightMySky · lightmysky.comW1-mt_Evw7zeJE6Q-s1

Answer key

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

The pile you only touch on top W1-mt_Evw7zeJE6Q-s1

  1. 9 · The pop removes 7, then 9 lands on top of 4.
  2. Last in, first out: the top is the only value within reach · Only the most recently added item can come out.
  3. Index minus one · Minus one looks at the end item and leaves it in place.
  4. That is exactly last in, first out applied to history · Unwinding history in reverse means newest first.
  5. Signal an error or refuse clearly, as decided upfront · An empty pop is an error, never a quiet made-up value.
  6. False · Leftovers mean some opener never closed, so the nesting is broken.
  7. Stacks reverse arrival order, so a queue is the right tool for arrival order · Newest first is the stack rule; arrival order needs first in, first out.
  8. B, then C, then A · Each pop takes the current top: B first, then C, then A.
Worksheet · LightMySky