This set of Data Structures & Algorithms Multiple Choice Questions & Answers (MCQs) focuses on “Evaluation of an Infix Expression (Not Parenthesized)”.
1. How many stacks are required for applying evaluation of infix expression algorithm?
Expression: Two stacks are required for evaluation of infix expression – one for operands and one for operators.
2. How many passes does the evaluation of infix expression algorithm makes through the input?
Explanation: Evaluation of infix expression algorithm is linear and makes only one pass through the input.
3. Identify the infix expression from the list of options given below.
Explanation: a/b+(c-d) is an infix expression since the operators are placed in between the operands.
4. Which of the following statement is incorrect with respect to evaluation of infix expression algorithm?
a) Operand is pushed on to the stack
b) If the precedence of operator is higher, pop two operands and evaluate
c) If the precedence of operator is lower, pop two operands and evaluate
d) The result is pushed on to the operand stack
Explanation: If the precedence of the operator is higher than the stack operator, then it is pushed on to the stack operator.
5. Evaluate the following statement using infix evaluation algorithm and choose the correct answer. 1+2*3-2
Explanation: According to precedence of operators, * is evaluated first. + and – have equal priorities. Hence, 1+6-2= 5.
6. Evaluation of infix expression is done based on precedence of operators.
Explanation: During evaluation of infix expression, the operators with higher precedence are evaluated first, followed by operators with lower precedence.
7. Of the following choices, which operator has the lowest precedence?
Explanation: The operator with the lowest precedence is #, preceded by +, / and then ^.
8. The system throws an error if parentheses are encountered in an infix expression evaluation algorithm.
Explanation: The algorithm holds good for infix expression with parentheses. The system does not throw error.
9. Evaluate the following and choose the correct answer.
a/b+c*d where a=4, b=2, c=2, d=1.
Explanation: * and / have higher priority. Hence, they are evaluated first. Then, + is evaluated. Hence, 2+2=4.
10. Evaluate the following statement using infix evaluation algorithm and choose the correct answer. 4*2+3-5/5
Explanation: 4*2 and 5/5 are evaluated first and then, 8+3-1 is evaluated and the result is obtained as 10.
11. Using the evaluation of infix expression, evaluate a^b+c and choose the correct answer. (a=2, b=2, c=2)
Explanation: ^ has the highest precedence. Hence, 2^2 is evaluated and then 4+2 gives 6.
12. Evaluate the following infix expression using algorithm and choose the correct answer. a+b*c-d/e^f where a=1, b=2, c=3, d=4, e=2, f=2.
Explanation: ^ has the highest order of precedence. Hence, 2^2 is evaluated first, and then, 2*3 and 4/4 are evaluated. Therefore, 1+6-1=6.
Explanation: From the given expression tree, the result of the infix expression is evaluated to be 12.
Sanfoundry Global Education & Learning Series – Data Structure.
To practice all areas of Data Structure, here is complete set of 1000+ Multiple Choice Questions and Answers.