Stack · Data Structures
13 questions · asked in 8 of 27 years (2000–2026)
MCQ 10 · MSQ 2 · NAT 11-mark 11 · 2-mark 2
2000Questions per year2026
Questions 6–10 of 13
The following postfix expression with single digit operands is evaluated using a stack:
8 2 3 ^ / 2 3 * + 5 1 * - Note that ^ is the exponentiation operator. The top two elements of the stack after the first * is evaluated are:Answer: (A)
Step by step:
- Push 8, 2, 3, then apply ^ the stack holds 8, 8 (2^3 = 8)
- Next is /, so 8 / 8 = 1
- Push 2 and 3, then apply * 2 3 = 6 (this is the first *)
The stack now holds 1, 6 (top = 6).
Answer: 6, 1
- Push 8, 2, 3, then apply ^ the stack holds 8, 8 (2^3 = 8)
- Next is /, so 8 / 8 = 1
- Push 2 and 3, then apply * 2 3 = 6 (this is the first *)
The stack now holds 1, 6 (top = 6).
Answer: 6, 1
A function defined on stacks of integers satisfies the following properties. and for all stacks and integers .
If a stack contains the integers 2, -3, 2, -1, 2 in order from bottom to top, what is ?
Answer: (C)
Each push gives , where i is the pushed integer and f(old) is the value for the stack before the push. Start with and push 2, -3, 2, -1, 2 in that order (bottom to top).
push 2:
push -3:
push 2:
push -1:
push 2:
So .
Answer: (C)
push 2:
push -3:
push 2:
push -1:
push 2:
So .
Answer: (C)
A program attempts to generate as many permutations as possible of the string, 'abcd' by pushing the characters a,b,c,d in the same order onto a stack, but it may pop off the top character at any time. Which one of the following strings CANNOT be generated using this program?
Answer: (D)
Characters go onto the stack in the order a, b, c, d. A pop can happen at any time, so the output is the pop order.
1. abcd: push a, pop a; push b, pop b; push c, pop c; push d, pop d. This is possible.
2. dcba: push a, b, c, d, then pop all. The pops give d, c, b, a. This is possible.
3. cbad: push a, b, c, then pop three times to get c, b, a. Then push d and pop d. This is possible.
4. cabd: to pop c first, a and b must already be pushed, so the stack holds a, b, c with c on top and a at the bottom. After c is popped, the top is b, and a is below it. So b has to come out before a, and the order c, a, b is not possible.
Answer: (D)
1. abcd: push a, pop a; push b, pop b; push c, pop c; push d, pop d. This is possible.
2. dcba: push a, b, c, d, then pop all. The pops give d, c, b, a. This is possible.
3. cbad: push a, b, c, then pop three times to get c, b, a. Then push d and pop d. This is possible.
4. cabd: to pop c first, a and b must already be pushed, so the stack holds a, b, c with c on top and a at the bottom. After c is popped, the top is b, and a is below it. So b has to come out before a, and the order c, a, b is not possible.
Answer: (D)
Assume that the operators +, -, x , are left associative and ^ is right associative. the order of precedence (from highest to lowest) is ^, x , +, -. The postfix expression corresponding to the infix expression a + b x c-d^e^f is
Answer: (A)
Precedence from highest: , , , . The operator is right associative, the others are left associative. So the expression groups as , where .
Scan left to right with an operator stack. Operands go straight to the output.
a: output a
+: stack empty, push +
b: output b
: higher than +, push
c: output c. Output so far:
: lower than and than +, so pop then +. Output: . Push
d: output d
: higher than , push
e: output e
: same as the on top and right associative, so push (do not pop)
f: output f
End: pop everything, giving . Result: .
(B) writes , which is , but is right associative. (C) adds a and b first, which ignores that x binds tighter than +. (D) puts operators next to the first operands and ends with operands, so it is not a valid postfix form.
Answer: (A)
Scan left to right with an operator stack. Operands go straight to the output.
a: output a
+: stack empty, push +
b: output b
: higher than +, push
c: output c. Output so far:
: lower than and than +, so pop then +. Output: . Push
d: output d
: higher than , push
e: output e
: same as the on top and right associative, so push (do not pop)
f: output f
End: pop everything, giving . Result: .
(B) writes , which is , but is right associative. (C) adds a and b first, which ignores that x binds tighter than +. (D) puts operators next to the first operands and ends with operands, so it is not a valid postfix form.
Answer: (A)
The best data structure to check whether an arithmetic expression has balanced parentheses is a
Answer: (B)
Use a stack. Read the expression from left to right.
- On a left parenthesis, push it.
- On a right parenthesis, pop from the stack. If the stack is empty at that moment, there is a right parenthesis with no match (as in ")("), so the expression is not balanced.
- At the end of the expression, the stack must be empty. If it is not, some left parenthesis has no match.
A right parenthesis closes the most recently opened parenthesis that is still open. That is the last one pushed, and a stack gives the last item pushed first (LIFO). A queue would give the oldest one, which is wrong for nested parentheses such as ((a)(b)).
Answer: (B)
- On a left parenthesis, push it.
- On a right parenthesis, pop from the stack. If the stack is empty at that moment, there is a right parenthesis with no match (as in ")("), so the expression is not balanced.
- At the end of the expression, the stack must be empty. If it is not, some left parenthesis has no match.
A right parenthesis closes the most recently opened parenthesis that is still open. That is the last one pushed, and a stack gives the last item pushed first (LIFO). A queue would give the oldest one, which is wrong for nested parentheses such as ((a)(b)).
Answer: (B)
Keep practising