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 45
push 92
pop
pop
push 48
push 82
pop
push 54
push 88
push 56
  1. A. 56, 88, 54, 48
  2. B. 82, 54, 88, 56
  3. C. 48, 54, 88, 56
  4. D. 48, 54, 88

Correct answer: C. 48, 54, 88, 56

Tracing push/pop on an empty stack in order gives, bottom-to-top, [48, 54, 88, 56]. Reading top-to-bottom instead gives the reversed trap [56, 88, 54, 48]; mistakenly treating "pop" as removing the front (as in a queue) gives [82, 54, 88, 56].

More Pseudocode and Code Tracing questions

Exams that ask this topic