ProgrammingHardTCS NQT · Infosys · Capgemini
What will be the output of the following pseudocode? (^ is exponentiation; / is integer division truncating toward zero. An operator pops the top element as b and the next as a, then pushes the value of a op b.)
What will be the output of the following pseudocode? (^ is exponentiation; / is integer division truncating toward zero. An operator pops the top element as b and the next as a, then pushes the value of a op b.)
Stack S = empty
For each token in "20 3 2 ^ 4 - / 7 3 - *"
If token is a number Then
push(S, token)
Else
b = pop(S)
a = pop(S)
push(S, a token b)
End If
End For
Print pop(S)- A. 12
- B. 16
- C. 4
- D. 10
Correct answer: B. 16
Evaluate left to right: 3^2 = 9; 9 - 4 = 5; 20 / 5 = 4; then 7 - 3 = 4; finally 4 * 4 = 16. Reading the operands in the wrong order would give a different value.
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 val[6] = {5, 8, 2, 9, 4, 7} Integer nxt[6] = {3, 5, 0, 1, -1, 4} Integer p =…
- What will be the output of the following pseudocode? Integer a[51] = all zeros (indices 0 to 50) For i = 1 to 50 For j = i to 50 step i…
- What will be the output of the following pseudocode? (ord('A') = 65; the letters are uppercase and the mod keeps every shift inside the…
- What will be the output of the following pseudocode? (Arrays are 0-indexed.) Integer a[7] = {5, 1, 4, 2, 8, 0, 3} Integer swaps = 0…
- What will be the output of the following pseudocode? (Static scoping: a name used inside a procedure refers to the variable visible where…
- What will be the output of the following pseudocode? (Arrays are 0-indexed.) Integer a[7] = {1, 2, 4, 5, 7, 9, 10} Integer i = 0, j = 6, c…