CBSE Class 12 Computer Science Chapter 9: Stack NCERT Solutions
This chapter provides NCERT Solutions for Class 12 Computer Science, focusing on the fundamental data structure: Stack. The solutions cover the evaluation of postfix expressions, a common application of stacks. Students will learn how to process these expressions step-by-step, demonstrating the status of the stack after each operation. This includes handling arithmetic operations and logical operations. The detailed explanations are designed to clarify the underlying principles of stack manipulation, making it easier for students to grasp the concepts. These solutions are essential for exam preparation, offering clear guidance on solving problems related to postfix expression evaluation and stack usage.
Quick info
| Board | CBSE |
|---|---|
| Class | Class 12 |
| Subject | Computer Science (C++) |
| Session | 2026 |
| Language | English |
| Type | NCERT Solutions |
| Chapter | Chapter 9 |
Chapter summary
Chapter 9 of the CBSE Class 12 Computer Science curriculum focuses on the Stack data structure. The provided NCERT Solutions offer detailed explanations for evaluating postfix expressions. Each solution meticulously shows the state of the stack after every operation, covering both arithmetic and logical operators. This chapter is crucial for understanding how stacks are used in practical programming scenarios, particularly in expression evaluation and compiler design.
Learning outcomes
- Understand the concept of a stack data structure.
- Learn to evaluate postfix expressions.
- Trace the status of a stack during expression evaluation.
- Apply stack operations (PUSH, POP) to solve problems.
- Differentiate between arithmetic and logical operations in postfix expressions.
Topics covered
Paper topics
- Stack Data Structure
- Postfix Expression Evaluation
- Stack Operations (PUSH, POP)
- Arithmetic Operations in Postfix
- Logical Operations in Postfix
- Stack Status Tracing
Important topics
- Postfix Expression Evaluation
- Stack Operations
- Tracing Stack Status
- Handling Arithmetic Operators
- Handling Logical Operators
PDF preview
Read page by page below. PDF is streamed from the official NCERT website — no download button on this page.
Questions and Solutions
Question 1
To evaluate the postfix expression <math>2,13, +, 5, -6,3,/5,*,<</math>, we use a stack. We process the expression from left to right. Operands are pushed onto the stack, and when an operator is encountered, operands are popped, the operation is performed, and the result is pushed back onto the stack.
Here is the step-by-step evaluation:
- Scan 2: Push 2 onto the stack. Stack: [2]
- Scan 13: Push 13 onto the stack. Stack: [2, 13]
- Scan +: Pop 13 and 2. Evaluate 2 + 13 = 15. Push 15. Stack: [15]
- Scan 5: Push 5 onto the stack. Stack: [15, 5]
- Scan -: Pop 5 and 15. Evaluate 15 - 5 = 10. Push 10. Stack: [10]
- Scan 6: Push 6 onto the stack. Stack: [10, 6]
- Scan 3: Push 3 onto the stack. Stack: [10, 6, 3]
- Scan /: Pop 3 and 6. Evaluate 6 / 3 = 2. Push 2. Stack: [10, 2]
- Scan 5: Push 5 onto the stack. Stack: [10, 2, 5]
- Scan *: Pop 5 and 2. Evaluate 2 * 5 = 10. Push 10. Stack: [10, 10]
- Scan <: Pop 10 and 10. Evaluate 10 < 10. This is FALSE. Push FALSE. Stack: [FALSE]
The final result of the expression is FALSE.
Question 2
We evaluate the postfix expression <math>100,40,8,/,20,10,-,+,*</math> using a stack. Operands are pushed, and operators trigger pop, operation, and push of the result.
Step-by-step evaluation:
- Scan 100: Push 100. Stack: [100]
- Scan 40: Push 40. Stack: [100, 40]
- Scan 8: Push 8. Stack: [100, 40, 8]
- Scan /: Pop 8 and 40. Evaluate 40 / 8 = 5. Push 5. Stack: [100, 5]
- Scan 20: Push 20. Stack: [100, 5, 20]
- Scan 10: Push 10. Stack: [100, 5, 20, 10]
- Scan -: Pop 10 and 20. Evaluate 20 - 10 = 10. Push 10. Stack: [100, 5, 10]
- Scan +: Pop 10 and 5. Evaluate 5 + 10 = 15. Push 15. Stack: [100, 15]
- Scan *: Pop 15 and 100. Evaluate 100 * 15 = 1500. Push 1500. Stack: [1500]
The final result of the expression is 1500.
Question 3
We evaluate the postfix expression <math>T, F, NOT, AND, T, OR, F, AND</math> using a stack. This expression involves logical operands (T for TRUE, F for FALSE) and logical operators (NOT, AND, OR).
Step-by-step evaluation:
- Scan T: Push TRUE. Stack: [TRUE]
- Scan F: Push FALSE. Stack: [TRUE, FALSE]
- Scan NOT: Pop FALSE. Evaluate NOT FALSE = TRUE. Push TRUE. Stack: [TRUE]
- Scan AND: Pop TRUE and TRUE. Evaluate TRUE AND TRUE = TRUE. Push TRUE. Stack: [TRUE]
- Scan T: Push TRUE. Stack: [TRUE, TRUE]
- Scan OR: Pop TRUE and TRUE. Evaluate TRUE OR TRUE = TRUE. Push TRUE. Stack: [TRUE]
- Scan F: Push FALSE. Stack: [TRUE, FALSE]
- Scan AND: Pop FALSE and TRUE. Evaluate TRUE AND FALSE = FALSE. Push FALSE. Stack: [FALSE]
The final result of the expression is FALSE.
Common mistakes
- Incorrectly performing PUSH or POP operations.
- Errors in handling operator precedence in postfix evaluation.
- Misinterpreting the order of operands for binary operations.
- Forgetting to show the stack status after each operation.
Revision tips
- Practice evaluating various postfix expressions manually, showing each stack state.
- Pay close attention to the order of operands when evaluating binary operations.
- Understand how logical operators (AND, OR, NOT) affect the stack.
- Review the examples to identify common pitfalls in stack manipulation.
Practice MCQs
Q1. What is the primary operation performed on a stack when evaluating a postfix expression?
Explanation: When evaluating a postfix expression, operands are pushed onto the stack, and when an operator is encountered, operands are popped for the operation, and the result is pushed back.
Q2. In postfix notation, where do operators appear relative to their operands?
Explanation: In postfix notation (Reverse Polish Notation), the operator follows its operands.
Q3. When evaluating '2, 13, +, 5, -, 6, 3, /, 5, *, <', what is the final result?
Explanation: The expression evaluates to FALSE because 10 < 10 is false.
Q4. What is the result of evaluating '100, 40, 8, /, 20, 10, -, +, *'?
Explanation: The expression evaluates to 1500 after performing division, subtraction, addition, and finally multiplication.
Q5. How is the logical NOT operation typically handled in postfix evaluation?
Explanation: The NOT operator is a unary operator; it pops a single operand, applies the logical NOT, and pushes the boolean result back onto the stack.
Frequently asked questions
What is a stack in the context of Computer Science?
A stack is a linear data structure that follows the Last-In, First-Out (LIFO) principle. Elements are added (pushed) and removed (popped) from the same end, known as the top.
What is a postfix expression?
A postfix expression, also known as Reverse Polish Notation (RPN), is an expression in which operators follow their operands. For example, 'a + b' in infix becomes 'a b +' in postfix.
How are postfix expressions evaluated using a stack?
When evaluating a postfix expression, operands are pushed onto a stack. When an operator is encountered, the required number of operands are popped from the stack, the operation is performed, and the result is pushed back onto the stack.
Why is it important to show the stack status after each operation?
Showing the stack status after each operation helps in understanding the step-by-step execution of the postfix expression evaluation and verifies the correctness of the process.
What is the difference between evaluating arithmetic and logical postfix expressions?
Arithmetic expressions involve numerical operands and operators like +, -, *, /. Logical expressions involve boolean operands (TRUE/FALSE) and operators like AND, OR, NOT. The evaluation process is similar, but the data types and operations differ.
Content reviewed by the NCERT Help team. Editorial Team and update policy
NCERT Solutions PDF PDF on NCERT Help. URL unchanged for search indexing.