7 Lecture
CS301
Midterm & Final Term Short Notes
Evaluating postfix expressions
Postfix notation is a mathematical notation where operators are written after their operands. Evaluating postfix expressions involves using a stack to keep track of operands and operators and performing the necessary calculations. The process in
Important Mcq's
Midterm & Finalterm Prepration
Past papers included
Download PDF
- What is postfix notation? a) Operators are written before their operands b) Operators are written after their operands c) Operators are written in between their operands d) None of the above
Answer: b) Operators are written after their operands
- Which data structure is used to evaluate postfix expressions? a) Queue b) Linked List c) Stack d) Tree
Answer: c) Stack
- Which of the following is an example of a postfix expression? a) 2 + 3 b) 2 * 3 + 4 c) 2 3 + d) (2 + 3) * 4
Answer: c) 2 3 +
- What is the first step in evaluating a postfix expression? a) Scan the expression from right to left b) Scan the expression from left to right c) Push the first operand onto the stack d) Pop the first operand from the stack
Answer: b) Scan the expression from left to right
- What happens when an operand is encountered in a postfix expression? a) It is pushed onto the stack b) It is popped from the stack c) It is ignored d) None of the above
Answer: a) It is pushed onto the stack
- What happens when an operator is encountered in a postfix expression? a) It is pushed onto the stack b) It is popped from the stack c) The top two operands are popped from the stack, and the operation is performed on them d) None of the above
Answer: c) The top two operands are popped from the stack, and the operation is performed on them
- Which of the following is an example of a postfix expression that involves multiplication? a) 2 + 3 b) 2 * 3 + 4 c) 2 3 * d) (2 + 3) * 4
Answer: c) 2 3 *
- What happens when the entire postfix expression has been scanned? a) The result is the top element in the stack b) The result is the bottom element in the stack c) The stack is empty d) None of the above
Answer: a) The result is the top element in the stack
- Which of the following is an example of a postfix expression that involves subtraction? a) 2 + 3 b) 2 * 3 + 4 c) 2 3 - d) (2 + 3) * 4
Answer: c) 2 3 -
- Which of the following is an example of a postfix expression that involves division? a) 2 + 3 b) 2 * 3 + 4 c) 2 3 / d) (2 + 3) * 4
Answer: c) 2 3 /
Subjective Short Notes
Midterm & Finalterm Prepration
Past papers included
Download PDF
What is postfix notation, and how is it different from infix notation? Answer: Postfix notation is a mathematical notation where operators are written after their operands. In contrast, infix notation is a notation where operators are written between their operands. The primary difference is that postfix notation eliminates the need for parentheses to indicate the order of operations.
How does a stack data structure help in evaluating postfix expressions? Answer: A stack is used to keep track of operands and operators and perform the necessary calculations. The process involves scanning the expression from left to right, pushing operands onto the stack, and when an operator is encountered, popping the top two operands off the stack, performing the operation, and pushing the result back onto the stack.
How do you evaluate a postfix expression? Answer: The process involves scanning the expression from left to right, pushing operands onto the stack, and when an operator is encountered, popping the top two operands off the stack, performing the operation, and pushing the result back onto the stack. The final result is the top element in the stack after all expressions have been evaluated.
What happens when an operand is encountered in a postfix expression? Answer: It is pushed onto the stack.
What happens when an operator is encountered in a postfix expression? Answer: The top two operands are popped from the stack, and the operation is performed on them. The result is then pushed back onto the stack.
What is the significance of the order of operations in postfix notation? Answer: The order of operations in postfix notation is determined by the order in which the operands and operators are encountered. Operators are applied to the two most recently pushed operands in the stack, so the order of operations is naturally enforced.
How can you detect and handle errors while evaluating a postfix expression? Answer: One common method is to check the stack after evaluating the expression. If there is more than one element left in the stack, it indicates an error. Another method is to check for errors while scanning the expression and handling them as they occur.
Can all mathematical expressions be converted to postfix notation? Answer: Yes, all mathematical expressions can be converted to postfix notation using the algorithm for conversion.
What are some advantages of using postfix notation? Answer: Postfix notation eliminates the need for parentheses to indicate the order of operations and can be evaluated efficiently using a stack data structure.
What are some limitations of using postfix notation? Answer: Postfix notation may be less intuitive to read and write than infix notation, and the conversion process can be time-consuming for complex expressions.
- Initialize an empty stack.
- Scan the expression from left to right.
- If an operand is encountered, push it onto the stack.
- If an operator is encountered, pop the top two operands off the stack, perform the operation, and push the result back onto the stack.
- Continue scanning the expression until all elements have been processed.
- The final result is the top element in the stack.