Skip to content

Stack

storing

Level 1 of 3

Also called Last in, first out.

fresh numbers

The test's questions that a run answers, asked again on new numbers whenever you like. The answer, its working and the reason for every other choice come from stack's own run on the numbers shown.

choose one

question 1

An empty stack is given 4, 9, -, 7, 2, -. Each number is put on, and each dash takes the top off. What does the pile hold then, bottom first?

the answer, and why

the answer is B

9 goes on 4 and comes straight back off. Then 7 and 2 go on, and the last take takes 2, the top.

Stack, newest out. Puts and takes happen at the right end, the top. The heavy line is the bottom.

put 44put 949take49gives 9put 747put 2472take472gives 2

Out, in turn: 9, 2.

  • just put in
  • just taken out
Each operation in turn. The pile ends holding 4 and 7.

if you chose another

  • That keeps 9, but the first take takes 9, the top then.
  • That takes 4, the bottom. A take only ever takes the top.
  • That misses the first take, which takes 9 off.

new numbers

question 2

How many numbers come back from an empty stack given 8, 3, -, -, -, 5, -?

the answer, and why

the answer is C

The first two takes give back 3, then 8. The third finds the pile empty and gives nothing. The last gives back 5.

Stack, newest out. Puts and takes happen at the right end, the top. The heavy line is the bottom.

put 88put 383take83gives 3take8gives 8takeempty, nothing comesput 55take5gives 5

Out, in turn: 3, 8, 5.

  • just put in
  • just taken out
The third take finds nothing, so 3 numbers come back.

if you chose another

  • That counts every dash, but the third take finds the pile empty.
  • That stops before 5 goes on and comes back.
  • That counts every operation, the puts too.

new numbers