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
- A. 40, 55, 96, 96, 95
- B. 53, 82, 96, 96, 95, 64
- C. 40, 55, 96, 96, 95, 64
- 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].
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- What is the final content of the queue (front to back, left to right) after this sequence of operations on an initially empty queue?…
- 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 tightest time complexity of the following pseudocode, in terms of n? (Treat any constant like 2 as a fixed constant, not…
- 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 = 65, y = 72 x = x ^ y y = x ^ y x = x ^ y Print x, y
- This function uses a counter that PERSISTS across calls (a static/global variable), starting at 1. What does each of the 4 calls print, in…