CBSE Class 12 Computer Science Chapter 9: Stack NCERT Solutions
This chapter provides comprehensive NCERT Solutions for Class 12 Computer Science, focusing on the fundamental data structure: Stack. It covers the essential concepts of stacks, including their LIFO (Last-In, First-Out) principle, and demonstrates how to perform various operations. The solutions meticulously explain the evaluation of postfix expressions, detailing the step-by-step status of the stack after each operation. This includes handling arithmetic and logical operators. These solutions are designed to help students understand the practical application of stacks in expression evaluation and prepare effectively for their board examinations by offering clear, concise, and accurate explanations.
Quick info
| Board | CBSE |
|---|---|
| Class | Class 12 |
| Subject | Computer Science |
| Session | 2026 |
| Language | English |
| Type | NCERT Solutions |
| Chapter | 9. Stack |
Chapter summary
Chapter 9 on Stacks for CBSE Class 12 Computer Science covers the LIFO data structure. The NCERT Solutions focus on evaluating postfix expressions, illustrating the stack's state with each operation. It includes examples with arithmetic and logical operators, providing a clear understanding of how stacks manage these computations. These solutions are crucial for grasping stack implementation and its role in expression processing.
Learning outcomes
- Understand the LIFO principle of stacks.
- Learn to perform push and pop operations on a stack.
- Evaluate postfix expressions using a stack.
- Trace the status of a stack during expression evaluation.
- Apply stack operations to solve problems involving arithmetic and logical expressions.
Topics covered
Paper topics
- Stack Data Structure
- LIFO Principle
- Stack Operations (Push, Pop)
- Postfix Expression Evaluation
- Arithmetic Operators in Postfix
- Logical Operators in Postfix
- Stack Status Tracking
Important topics
- Postfix Expression Evaluation
- Stack Operations
- LIFO Principle
- Tracking Stack Status
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
We will evaluate the postfix expression by processing it from left to right and using a stack to store operands. The status of the stack after each operation is shown below:
| Item Processed | Operation | Stack Status (Bottom to Top) |
|---|---|---|
| 2 | PUSH 2 | 2 |
| 13 | PUSH 13 | 2, 13 |
| + | POP 13, POP 2. Evaluate . PUSH 15. | 15 |
| 5 | PUSH 5 | 15, 5 |
| - | POP 5, POP 15. Evaluate . PUSH 10. | 10 |
| 6 | PUSH 6 | 10, 6 |
| 3 | PUSH 3 | 10, 6, 3 |
| / | POP 3, POP 6. Evaluate . PUSH 2. | 10, 2 |
| 5 | PUSH 5 | 10, 2, 5 |
| * | POP 5, POP 2. Evaluate . PUSH 10. | 10, 10 |
| < | POP 10, POP 10. Evaluate which is FALSE. PUSH FALSE. | FALSE |
The final result of the expression evaluation is FALSE.
Question 2
We evaluate the given postfix expression using a stack. Operands are pushed onto the stack, and when an operator is encountered, the required operands are popped, the operation is performed, and the result is pushed back. The stack's state is tracked after each step:
| Item Processed | Operation | Stack Status (Bottom to Top) |
|---|---|---|
| 100 | PUSH 100 | 100 |
| 40 | PUSH 40 | 100, 40 |
| 8 | PUSH 8 | 100, 40, 8 |
| / | POP 8, POP 40. Evaluate . PUSH 5. | 100, 5 |
| 20 | PUSH 20 | 100, 5, 20 |
| 10 | PUSH 10 | 100, 5, 20, 10 |
| - | POP 10, POP 20. Evaluate . PUSH 10. | 100, 5, 10 |
| + | POP 10, POP 5. Evaluate . PUSH 15. | 100, 15 |
| * | POP 15, POP 100. Evaluate . PUSH 1500. | 1500 |
The final result of the expression evaluation is 1500.
Question 3
We evaluate the postfix expression using a stack, processing operands and operators. 'T' represents True and 'F' represents False. The stack's state is tracked after each step:
| Item Processed | Operation | Stack Status (Bottom to Top) |
|---|---|---|
| T | PUSH T | T |
| F | PUSH F | T, F |
| NOT | POP F. Evaluate which is T. PUSH T. | T, T |
| AND | POP T, POP T. Evaluate which is T. PUSH T. | T |
| T | PUSH T | T, T |
| OR | POP T, POP T. Evaluate which is T. PUSH T. | T |
| F | PUSH F | T, F |
| AND | POP F, POP T. Evaluate which is F. PUSH F. | F |
The final result of the expression evaluation is F (False).
Common mistakes
- Incorrectly applying the LIFO principle.
- Errors in handling operator precedence during postfix evaluation.
- Mismanaging the stack state (e.g., popping from an empty stack).
- Confusing operand and operator roles in expression evaluation.
Revision tips
- Practice tracing stack operations manually for each example.
- Focus on understanding the order of operations in postfix expressions.
- Re-solve the given examples without looking at the solutions.
- Pay close attention to how operands and operators interact on the stack.
Practice MCQs
Q1. What is the primary principle governing a stack data structure?
Explanation: Stacks operate on the LIFO principle, meaning the last element added is the first one to be removed.
Q2. In postfix notation, where do operators appear relative to their operands?
Explanation: In postfix notation (e.g., AB+), the operator follows its operands.
Q3. When evaluating a postfix expression, what action is taken when an operator is encountered?
Explanation: An operator in postfix evaluation requires popping the top two elements, applying the operator, and pushing the result back.
Q4. What is the result of evaluating the postfix expression '2, 3, +, 5, -'?
Explanation: 2 3 + -> 5; 5 5 - -> 0. The stack operations are: push 2, push 3, pop 3, pop 2, add (5), push 5, push 5, pop 5, pop 5, subtract (0), push 0. Result is 0.
Q5. If the stack contains [10, 5] and the operation is '-', what is the result?
Explanation: Assuming the expression implies 10 - 5, the top element (5) is popped, then the next element (10) is popped. The operation 10 - 5 yields 5, which is then pushed back.
Frequently asked questions
What is a stack in 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 of the stack.
How is a stack used to evaluate postfix expressions?
When evaluating a postfix expression, we scan the expression from left to right. Operands are pushed onto the stack. When an operator is encountered, the top two operands are popped, the operation is performed, and the result is pushed back onto the stack.
What does 'LIFO' mean in the context of a stack?
LIFO stands for Last-In, First-Out. This means the most recently added element to the stack will be the first one to be removed.
Why is it important to show the status of the stack after each operation?
Showing the stack status after each operation helps in understanding the step-by-step execution of the algorithm, verifying the correctness of the evaluation, and identifying potential errors in the process.
Can stacks be used for logical operations as well as arithmetic ones?
Yes, stacks can be used to evaluate expressions involving logical operators (like AND, OR, NOT) in a similar manner to arithmetic operators, by popping operands, applying the logical operation, and pushing the boolean result.
Content reviewed by the NCERT Help team. Editorial Team and update policy
NCERT Solutions PDF PDF on NCERT Help. URL unchanged for search indexing.