ProgrammingHardTCS NQT · Infosys · Capgemini 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 89
pop
push 55
push 85
pop
push 91
push 81
push 77
pop
pop
  1. A. 55, 91, 81
  2. B. 81, 77
  3. C. 55, 91
  4. D. 91, 55

Correct answer: C. 55, 91

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

More Pseudocode and Code Tracing questions

Exams that ask this topic