ProgrammingHardAccenture · Cognizant pattern

What is the final content of the stack (bottom to top, left to right) after this sequence of operations on an initially empty stack?

What is the final content of the stack (bottom to top, left to right) after this sequence of operations on an initially empty stack?
push 60
pop
push 9
push 18
push 79
push 6
push 49
push 10
pop
  1. A. 49, 6, 79, 18, 9
  2. B. 9, 18, 79, 6, 49
  3. C. 9, 18, 79, 6, 49, 10
  4. D. 18, 79, 6, 49, 10

Correct answer: B. 9, 18, 79, 6, 49

Tracing push/pop on an empty stack in order gives, bottom-to-top, [9, 18, 79, 6, 49]. Reading top-to-bottom instead gives the reversed trap [49, 6, 79, 18, 9]; mistakenly treating "pop" as removing the front (as in a queue) gives [18, 79, 6, 49, 10].

More Pseudocode and Code Tracing questions

Exams that ask this topic