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
  1. 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

  1. Which data structure is used to evaluate postfix expressions? a) Queue b) Linked List c) Stack d) Tree

Answer: c) Stack

  1. 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 +

  1. 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

  1. 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

  1. 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

  1. 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 *

  1. 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

  1. 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 -

  1. 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
  1. 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.

  2. 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.

  3. 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.

  4. What happens when an operand is encountered in a postfix expression? Answer: It is pushed onto the stack.

  5. 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.

  6. 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.

  7. 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.

  8. 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.

  9. 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.

  10. 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.

Postfix notation is a mathematical notation where operators are written after their operands. Evaluating postfix expressions involves using a stack data structure 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. One advantage of using postfix notation is that it eliminates the need for parentheses to indicate the order of operations. 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. To evaluate a postfix expression, we can use the following algorithm:
  1. Initialize an empty stack.
  2. Scan the expression from left to right.
  3. If an operand is encountered, push it onto the stack.
  4. If an operator is encountered, pop the top two operands off the stack, perform the operation, and push the result back onto the stack.
  5. Continue scanning the expression until all elements have been processed.
  6. The final result is the top element in the stack.
It is important to handle errors while evaluating postfix expressions. 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. While all mathematical expressions can be converted to postfix notation using the algorithm for conversion, it may be less intuitive to read and write than infix notation. Additionally, the conversion process can be time-consuming for complex expressions. Nonetheless, evaluating postfix expressions can be done efficiently using a stack data structure and can be useful in certain applications.