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
- A. 56, 88, 54, 48
- B. 82, 54, 88, 56
- C. 48, 54, 88, 56
- 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].
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? (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 = 68, y = 64 x = x ^ y y = x ^ y x = x ^ y Print x, y
- What will be the output of the following pseudocode? Function isEven(Integer n) if (n == 0) return true end if return isOdd(n - 1) end…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division.) Function f(Integer n, Integer…
- What will be the output of the following pseudocode? Integer c = 0 for (each i from 1 to 14) for (each j from i + 1 to 14) if ((i + j) mod…
- What will be the output of the following pseudocode? (& is bitwise AND, >> is right shift.) Integer n = 3158, c = 0 while (n > 0) c = c +…