ProgrammingHardAmazon OA · Capgemini
What will be the output of the following pseudocode? (/ is integer division)
What will be the output of the following pseudocode? (/ is integer division)
Stack st = empty
for each token t in "6 2 3 + - 3 8 2 / + *"
if (t is a number)
push(st, t)
else
a = pop(st)
b = pop(st)
push(st, b t a) // apply operator t as b t a
end if
end for
Print pop(st)- A. -3
- B. 21
- C. 7
- D. 1
Correct answer: C. 7
6 − (2 + 3) = 1, then 3 + 8 / 2 = 7, and 1 × 7 = 7. Popping in the wrong order (a t b) gives −1 and 3 + 0, i.e. the trap −3.
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? Integer a = 144, b = 89, t, steps = 0 while (b != 0) t = a mod b a = b b = t steps =…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division; , / and mod have equal…
- What will be the output of the following pseudocode? (& is bitwise AND, >> is right shift) Integer s = 0, x, t for (each i from 1 to 16) x…
- What will be the output of the following pseudocode? Integer m[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} for (each i from 0 to 2) for…
- What will be the output of the following pseudocode? Integer n = 0, c = 0 do c = c + 1 n = n + 3 while (n mod 5 != 0) Print c, n
- What will be the output of the following pseudocode? Integer cat[0..6] cat[0] = 1 for (each i from 1 to 6) cat[i] = 0 for (each j from 0…