Data Structures Questions and Answers Part-13

1. What is the result of the following postfix expression?
ab*cd*+ where a=2,b=2,c=3,d=4.
a) 16
b) 12
c) 14
d) 10

Answer: a
Explanation: The infix expression is a*b+c*d. Evaluating it, we get, 2*2+3*4=16.

2. Consider the stack
| 5 |
| 4 |
| 3 |
| 2 |.
At this point, ‘*’ is encountered. What has to be done?
a) 5*4=20 is pushed into the stack
b) * is pushed into the stack
c) 2*3=6 is pushed into the stack
d) * is ignored

Answer: a
Explanation: When an operator is encountered, the first two operands of the stack are popped, evaluated and the result is pushed into the stack.

3. Evaluate the postfix expression ab + cd/- where a=5, b=4, c=9, d=3.
a) 23
b) 15
c) 6
d) 10

Answer: c
Explanation: The infix expression is (a+b)-c/d. Evaluating it, (5+4)-9/3 gives 6.

4. Evaluate and write the result for the following postfix expression
abc*+de*f+g*+ where a=1, b=2, c=3, d=4, e=5, f=6, g=2.
a) 61
b) 59
c) 60
d) 55

Answer: b
Explanation: The infix expression is a+b*c+(d*e+f)*g. Evaluating it, 1+2*3+(4*5+6)*2 gives 59.

5. For the given expression tree, write the correct postfix expression.
15
a) abc*+
b) abc+*
c) ab+c*
d) a+bc*

Answer: a
Explanation: Evaluating the given expression tree gives the infix expression a+b*c. Converting it to postfix, we get, abc*+.

6. What data structure is used when converting an infix notation to prefix notation?
a) Stack
b) Queue
c) B-Trees
d) Linked-list

Answer: a
Explanation: First you reverse the given equation and carry out the algorithm of infix to postfix expression. Here, the data structure used is stacks.

7. What would be the Prefix notation for the given equation?
A+(B*C)
a) +A*CB
b) *B+AC
c) +A*BC
d) *A+CB

Answer: c
Explanation: Reverse the equation or scan the equation from right to left. Apply the infix-postfix algorithm. The equation inside the bracket evaluates to CB* and outside the bracket evaluates to A+ therefore getting CB*A+. Reversing this and we get +A*BC.

8. What would be the Prefix notation for the given equation?
(A*B)+(C*D)
a) +*AB*CD
b) *+AB*CD
c) **AB+CD
d) +*BA*CD

Answer: a
Explanation: Reverse the equation or scan the equation from right to left. Apply the infix-postfix algorithm. The equation inside the brackets evaluate to DC* and BA* respectively giving us DC*BA*+ in the end. Reversing this we get the +*AB*CD.

9. What would be the Prefix notation for the given equation?
A+B*C^D
a) +A*B^CD
b) +A^B*CD
c) *A+B^CD
d) ^A*B+CD

Answer: a
Explanation: Reverse the equation or scan the equation from right to left. Apply the infix-prefix algorithm. The preference order in ascending order are as follows +*^. Operators are pushed into the stack and popped if its preference is greater than the one which is getting pushed. In the end all operators are popped. The equation evaluates to DC^B*A+. Reversing this we get our following answer.

10. Out of the following operators (^, *, +, &, $), the one having highest priority is _________
a) +
b) $
c) ^
d) &

Answer: c
Explanation: According to the algorithm (infix-prefix), it follows that the exponentiation will have the highest priority.