ProgrammingVery hardAmazon OA · TCS Digital
What will be the output of the following pseudocode? (/ is integer division)
What will be the output of the following pseudocode? (/ is integer division)
String tok[9] = {"-", "*", "+", "2", "3", "4", "/", "8", "2"}
Stack st = empty
for (each i from 8 down to 0)
if (tok[i] is a number)
push(st, tok[i])
else
Integer x = pop(st)
Integer y = pop(st)
push(st, x tok[i] y) // first popped is the LEFT operand
end if
end for
Print pop(st)- A. 4
- B. 16
- C. 24
- D. 20
Correct answer: B. 16
Scanning a prefix expression from the right: 8 / 2 = 4, 2 + 3 = 5, 5 * 4 = 20, and finally 20 − 4 = 16, i.e. (2 + 3) × 4 − 8 / 2.
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? Stack s = empty, mins = empty Procedure push2(Integer x) push(s, x) if (mins is empty…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer a[8] = {1, 3, -1, -3, 5, 3, 6, 7}, k = 3 Deque d…
- What will be the output of the following pseudocode? (& is AND, >> is right shift) Integer x = 23, t = 0 while ((x & 1) == 1) t = t + 1 x…
- What will be the output of the following pseudocode? (& is AND, | is OR, << and >> are shifts) Integer x = 43 Integer y = ((x & 170) >> 1)…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division; & is AND, ^ is XOR, | is OR…
- What will be the output of the following pseudocode? Integer n = 3524, steps = 0 while (n != 6174) Integer big = digits of n sorted in…