ProgrammingHardAmazon OA · TCS Digital
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
String in = "A+B*C-D/E", out = ""
Stack st = empty
// prec: * and / are 2, + and - are 1
for (each ch in in)
if (ch is a letter)
out = out + ch
else
while (st not empty AND prec(top(st)) >= prec(ch))
out = out + pop(st)
end while
push(st, ch)
end if
end for
while (st not empty)
out = out + pop(st)
end while
Print out- A. ABC*+DE/-
- B. AB+C*DE/-
- C. ABC*+D-E/
- D. ABCDE*/+-
Correct answer: A. ABC*+DE/-
This is infix-to-postfix conversion with left-to-right associativity. B*C is emitted before + leaves the stack; when - arrives it pops * and +; then D/E follows. Result: ABC*+DE/-.
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? (All variables are integers, so / is integer division; heap[k / 2] is the parent of…
- What will be the output of the following pseudocode? Integer table[7] = all -1, probes = 0, h for each key in {50, 700, 76, 85, 92, 73…
- What is the time complexity of the following pseudocode, as a function of n? for (i = 1; i < n; i = i 2) for (j = 0; j < i; j = j + 1)…
- What is the time complexity of the following pseudocode, as a function of n (n > 2)? while (n > 2) n = squareRoot(n) // integer square…
- What will be the output of the following pseudocode? Integer coins[3] = {1, 2, 5} Integer ways[0..6] = all 0 ways[0] = 1 for (each a from…
- What will be the output of the following pseudocode? (code(c) gives a letter's character code; strings are indexed from 0) String s =…