ProgrammingHardTCS 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 87 push 11 pop push 58 push 74 push 97 push 9 push 5 push 95
- A. 87, 58, 74, 97, 9, 5
- B. 11, 58, 74, 97, 9, 5, 95
- C. 95, 5, 9, 97, 74, 58, 87
- D. 87, 58, 74, 97, 9, 5, 95
Correct answer: D. 87, 58, 74, 97, 9, 5, 95
Tracing push/pop on an empty stack in order gives, bottom-to-top, [87, 58, 74, 97, 9, 5, 95]. Reading top-to-bottom instead gives the reversed trap [95, 5, 9, 97, 74, 58, 87]; mistakenly treating "pop" as removing the front (as in a queue) gives [11, 58, 74, 97, 9, 5, 95].
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division. Array is sorted and indexed…
- 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…
- What is the final content of the queue (front to back, left to right) after this sequence of operations on an initially empty queue?…
- Consider this naive recursive Fibonacci. How many TOTAL calls to fib (including the initial call) happen when computing fib(5)? Function…
- What will be the output of the following pseudocode? (Arrays are passed and assigned BY REFERENCE: b = a makes b point to the same array…
- What will be the output of the following pseudocode? (^ is bitwise XOR.) Integer x = 90, y = 62 x = x ^ y y = x ^ y x = x ^ y Print x, y