This page contains the NCERT Computer Science class 12 chapter 3 Stack. You can find the solutions for the chapter 3 of NCERT class 12 Computer Science Exercise. So is the case if you are looking for NCERT class 12 Computer Science related topic Stack questions and answers for the Exercise
Exercise
Question 1
1. State TRUE or FALSE for the following cases:
a)
Stack is a linear data structure ✔
b)
Stack does not follow LIFO rule ❌
c)
PUSH operation may result into underflow condition ❌
d)
In POSTFIX notation for expression, operators are placed after operands ✔
Answer 1
a) Stack is a linear data structure
✅ TRUE — Elements are organised in a sequence (linear order).
b) Stack does not follow LIFO rule
❌ FALSE — Stack follows LIFO (Last-In-First-Out).
c) PUSH operation may result into underflow condition
❌ FALSE — Underflow happens when we do POP on an empty stack. PUSH can cause overflow (in fixed-size stacks).
d) In POSTFIX notation for expression, operators are placed after operands
✅ TRUE — Postfix is also called Reverse Polish notation.
Question 2
2. Find the output of the following code:
a)
result=0;numberList=[10,20,30]numberList.append(40)result=result+numberList.pop()result=result+numberList.pop()print("Result=",result)b)
answer=[]; output=''answer.append('T')answer.append('A')answer.append('M')ch=answer.pop()output=output+chch=answer.pop()output=output+chch=answer.pop()output=output+chprint("Result=",output)Answer 2
a) Code
# Initialize result and list
result = 0
numberList = [10, 20, 30]
# Add an element to the end
numberList.append(40)
# Pop last two elements and add to result
result = result + numberList.pop() # adds 40; list becomes [10, 20, 30]
result = result + numberList.pop() # adds 30; list becomes [10, 20]
print("Result=", result)
Output:
Result= 70
b) Code
# Initialize stack and output string
answer = []
output = ''
# Push characters onto stack
answer.append('T')
answer.append('A')
answer.append('M')
# Pop characters to build output in reverse
ch = answer.pop() # pops 'M'; stack becomes ['T', 'A']
output = output + ch # output = "M"
ch = answer.pop() # pops 'A'; stack becomes ['T']
output = output + ch # output = "MA"
ch = answer.pop() # pops 'T'; stack becomes []
output = output + ch # output = "MAT"
print("Result=", output)
Output:
Result= MAT
Question 3
3. Write a program to reverse a string using stack.
Answer 3
Program (reverse a string using stack):
# Program to reverse a string using Stack (LIFO)
# PUSH operation
def opPush(stk, item):
stk.append(item) # add item to top of stack
# POP operation
def opPop(stk):
if len(stk) == 0:
print("underflow") # stack is empty
return None
return stk.pop() # remove and return top element
# ---------- Main Program ----------
text = input("Enter a string: ")
stack = [] # empty stack
# PUSH each character into stack
for ch in text:
opPush(stack, ch) # push characters in order
# POP characters to get reversed string
rev = ""
while True:
ch = opPop(stack)
if ch is None:
break
rev = rev + ch # build reversed string
print("Reversed string:", rev)
Sample Output:
Enter a string: EDUXIR
Reversed string: RIXUDE
Question 4
4. For the arithmetic expression
((2+3)*(4/2))+2
Show the step-by-step process for matching parentheses using stack data structure.
Answer 4
While executing a program, the compiler checks for matched parentheses, i.e., each opening parenthesis
( should have a corresponding closing parenthesis ) and the parentheses should be properly nested. If parentheses are mismatched, the compiler needs to throw an error. To handle matching of parentheses, a stack (LIFO) is used.We use PUSH for an opening bracket
( and POP for a closing bracket ). Other symbols (numbers/operators) are read but ignored for stack operation.Rule used (Stack / LIFO):
•
If we read
( → PUSH it into stack (at top).•
If we read
) → POP one ( from stack.•
If stack is empty and we try to POP → underflow → parentheses are mismatched
•
At the end, if stack is empty → parentheses are matched and properly nested; if not empty → some
( are left unmatched.Step-by-step matching using stack:
Representation: stack shown from bottom → top; top is on the right.
Step
Symbol Read
Action on Stack
Stack Content
(bottom → top)
(bottom → top)
1
(
PUSH (
(
2
(
PUSH (
( (
3
2
Read → Ignore
( (
4
+
Read → Ignore
( (
5
3
Read → Ignore
( (
6
)
POP
(
7
*
Read → Ignore
(
8
(
PUSH (
( (
9
4
Read → Ignore
( (
10
/
Read → Ignore
( (
11
2
Read → Ignore
( (
12
)
POP
(
13
)
POP
empty
14
+
Read → Ignore
empty
15
2
Read → Ignore
empty
Final Result:
•
After reading the complete expression, the stack is empty.
✅ Therefore, parentheses are properly matched and properly nested.
(Note: At no step did we try to POP from an empty stack, so no underflow occurred.)
Question 5
5. Evaluate the following postfix expressions while showing status of stack after each operation, given A=3, B=5, C=1, D=4:
a)
A B + C *
b)
A B * C / D *
Answer 5
Given: A = 3, B = 5, C = 1, D = 4
Method (Postfix using Stack / LIFO):
•
If an operand (number/variable) is read → PUSH into stack.
•
If an operator is read → POP two elements (second POP is the left operand), apply operator, then PUSH result.
a) Postfix Expression: A B + C *
Substitute values: 3 5 + 1 *
Step
Symbol Read
Operation Performed
Stack (Bottom → Top)
1
A (=3)
PUSH 3
3
2
B (=5)
PUSH 5
3, 5
3
+
POP 5 and 3 → 3+5=8, PUSH 8
8
4
C (=1)
PUSH 1
8, 1
5
*
POP 1 and 8 → 8×1=8, PUSH 8
8
Result: 8
b) Postfix Expression: A B * C / D *
Substitute values: 3 5 * 1 / 4 *
Step
Symbol Read
Operation Performed
Stack (Bottom → Top)
1
A (=3)
PUSH 3
3
2
B (=5)
PUSH 5
3, 5
3
*
POP 5 and 3 → 3×5=15, PUSH 15
15
4
C (=1)
PUSH 1
15, 1
5
/
POP 1 and 15 → 15÷1=15, PUSH 15
15
6
D (=4)
PUSH 4
15, 4
7
*
POP 4 and 15 → 15×4=60, PUSH 60
60
Result: 60
Question 6
6. Convert the following infix notations to postfix notations, showing stack and string contents at each step:
a)
A + B – C * D
b)
A * (( C + D)/E)
Answer 6
Answer 6:
a) A + B – C * D
Symbol
Postfix String
Stack
A
A
[]
+
A
[+]
B
AB
[+]
–
AB+
[-]
C
AB+C
[-]
*
AB+C
[-, *]
D
AB+CD
[-, *]
End
AB+CD*-
[]
Postfix: AB+CD*-
b) A * (( C + D)/E)
Symbol
Postfix String
Stack
A
A
[]
*
A
[*]
(
A
[*, (]
(
A
[*, (, (]
C
AC
[*, (, (]
+
AC
[*, (, (, +]
D
ACD
[*, (, (, +]
)
ACD+
[*, (]
/
ACD+
[*, (, /]
E
ACD+E
[*, (, /]
)
ACD+E/
[*]
End
ACD+E/*
[]
Postfix: ACD+E/*
Question 7
7. Write a program to create a Stack for storing only odd numbers out of all the numbers entered by the user, display the content of the Stack along with the largest odd number in the Stack, and keep popping elements while maintaining the largest element so far until the Stack is empty.
Answer 7
Answer 7:
Program (store only odd numbers and find the largest):
# Stack program to store only ODD numbers and find the largest odd number
# Uses PUSH (append) and POP (pop) as per stack (LIFO)
def isEmpty(stk):
return len(stk) == 0
def opPush(stk, item):
stk.append(item)
def opPop(stk):
if isEmpty(stk):
print("underflow")
return None
return stk.pop()
def display(stk):
# display from TOP to bottom
if isEmpty(stk):
print("Stack is empty")
return
print("Stack contents (TOP to bottom):")
for i in range(len(stk) - 1, -1, -1):
print(stk[i])
# -------- Main Program --------
n = int(input("How many numbers do you want to enter? "))
oddStack = [] # empty stack
for i in range(n):
num = int(input("Enter number " + str(i + 1) + ": "))
if num % 2 != 0: # odd number
opPush(oddStack, num)
# Display stack content
display(oddStack)
# Find largest odd by popping all elements
if isEmpty(oddStack):
print("No odd numbers were entered, so largest odd does not exist.")
else:
largest = opPop(oddStack) # first odd becomes largest initially
while not isEmpty(oddStack):
item = opPop(oddStack)
if item > largest:
largest = item
print("Largest odd number in the stack is:", largest)
Sample Output:
How many numbers do you want to enter? 6
Enter number 1: 12
Enter number 2: 7
Enter number 3: 18
Enter number 4: 5
Enter number 5: 9
Enter number 6: 4
Stack contents (TOP to bottom):
9
5
7
Largest odd number in the stack is: 9