ProgrammingVery hardTCS NQT · Accenture 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 40
push 55
push 53
pop
push 82
pop
push 96
push 96
push 95
push 64
  1. A. 40, 55, 96, 96, 95
  2. B. 53, 82, 96, 96, 95, 64
  3. C. 40, 55, 96, 96, 95, 64
  4. D. 64, 95, 96, 96, 55, 40

Correct answer: C. 40, 55, 96, 96, 95, 64

Tracing push/pop on an empty stack in order gives, bottom-to-top, [40, 55, 96, 96, 95, 64]. Reading top-to-bottom instead gives the reversed trap [64, 95, 96, 96, 55, 40]; mistakenly treating "pop" as removing the front (as in a queue) gives [53, 82, 96, 96, 95, 64].

More Pseudocode and Code Tracing questions

Exams that ask this topic