CBSE Class 12
Computer Science
03 Stack
30
Total
0
Attempted
0
Correct
0
Wrong
00:00:00
Q-1
Consider the following Python code implementing a stack using a list. What will be printed?
Medium
Implementation of Stack in Python
Reference Code
```python
stack = []
stack.append(10)
stack.append(20)
stack.append(30)
print(stack.pop(), stack)
A
30 [10, 20]
B
20 [10, 30]
C
30 [10, 20, 30]
D
10 [20, 30]
Answer: 30 [10, 20]
Explanation: pop() removes and returns the top element (30), leaving [10,20].
Q-2
A stack initially contains: [5, 8, 12]. After performing push(15), pop(), push(20), what is the final stack?
Medium
Operations on Stack
A
[5,8,12,20]
B
[5,8,12,15,20]
C
[5,8,12,15]
D
[5,8,12]
Answer: [5,8,12,15,20]
Explanation: push(15) → [5,8,12,15], pop() removes 15 → [5,8,12], push(20) → [5,8,12,20].
Q-3
What will be the output of the following stack operations?
Medium
Implementation of Stack in Python
Reference Code
```python
stack=[1,2,3]
stack.append(4)
stack.pop()
stack.append(5)
print(stack)
A
[1,2,3]
B
[1,2,3,4]
C
[1,2,3,5]
D
[1,2,4,5]
Answer: [1,2,3,5]
Explanation: Append 4 → [1,2,3,4], pop removes 4 → [1,2,3], append 5 → [1,2,3,5].
Q-4
Which expression correctly represents postfix notation of the infix expression: A + B * C?
Medium
Conversion from Infix to Postfix Notation
A
AB+C*
B
A+BC*
C
ABC+*
D
ABC*+
Answer: ABC*+
Explanation: Multiplication has higher precedence. So B*C evaluated first → ABC*+, then add A.
Q-5
Consider the stack operations below. What will be printed?
Medium
Implementation of Stack in Python
Reference Code
```python
s=[]
for i in range(3):
s.append(i)
print(s.pop(), s)
A
2 [0,1]
B
1 [0,2]
C
2 [0,1,2]
D
0 [1,2]
Answer: 2 [0,1]
Explanation: Stack becomes [0,1,2]; pop removes 2 leaving [0,1].
Q-6
What will be the result of evaluating the postfix expression: 5 2 3 * + ?
Medium
Evaluation of Postfix Expression
A
11
B
11
C
13
D
25
Answer: 11
Explanation: 2*3=6 → 5+6=11.
Q-7
What will be printed?
Medium
Stack
Reference Code
```python
stack=[10,20,30]
print(stack[-1])
A
10
B
20
C
30
D
Error
Answer: 30
Explanation: stack[-1] accesses the top element of the stack.
Q-8
Which postfix expression corresponds to infix: (A + B) * C?
Medium
Conversion from Infix to Postfix Notation
A
AB+C*
B
ABC+*
C
AB*C+
D
ABC*+
Answer: ABC*+
Explanation: Parentheses evaluated first → AB+ then multiplied with C → AB+C*.
Q-9
What will be printed?
Easy
Implementation of Stack in Python
Reference Code
```python
stack=[2,4,6]
stack.append(8)
print(len(stack))
A
4
B
3
C
5
D
2
Answer: 4
Explanation: After append(8) stack length becomes 4.
Q-10
Which data structure principle does stack follow?
Easy
Stack
A
FIFO
B
LIFO
C
LILO
D
FILO
Answer: LIFO
Explanation: Stack follows Last-In First-Out (LIFO).
Q-11
Evaluate postfix expression: 6 2 / 3 +
Medium
Evaluation of Postfix Expression
A
6
B
9
C
6
D
12
Answer: 6
Explanation: 6/2=3 then 3+3=6.
Q-12
What will be printed?
Medium
Operations on Stack
Reference Code
```python
s=[1,3,5]
s.append(7)
s.append(9)
s.pop()
print(s)
A
[1,3,5]
B
[1,3,5,7,9]
C
[1,3,5,9]
D
[1,3,5,7]
Answer: [1,3,5,7]
Explanation: append 7, append 9, pop removes 9 leaving [1,3,5,7].
Q-13
Which Python list operation represents PUSH in stack?
Easy
Implementation of Stack in Python
A
append()
B
insert()
C
add()
D
push()
Answer: append()
Explanation: append() adds element to top of stack.
Q-14
Which Python list operation represents POP in stack?
Easy
Operations on Stack
A
remove()
B
pop()
C
delete()
D
discard()
Answer: pop()
Explanation: pop() removes the top element.
Q-15
Convert the infix expression A * (B + C) to postfix.
Medium
Conversion from Infix to Postfix Notation
A
ABC+*
B
AB*C+
C
AB+C*
D
A*BC+
Answer: AB+C*
Explanation: Parentheses first: B+C → BC+, then multiply by A → ABC+*.
Q-16
What will be printed?
Medium
Implementation of Stack in Python
Reference Code
```python
s=[10,20]
s.append(30)
s.append(40)
s.pop()
print(s[-1])
A
10
B
20
C
30
D
40
Answer: 40
Explanation: Stack becomes [10,20,30]; top element is 30.
Q-17
If stack contains [2,4,6] and we perform pop() twice, what remains?
Medium
Operations on Stack
A
[2]
B
[4]
C
[6]
D
[]
Answer: [2]
Explanation: First pop removes 6, second pop removes 4 leaving [2].
Q-18
Evaluate postfix expression: 3 4 + 2 *
Medium
Evaluation of Postfix Expression
A
14
B
14
C
10
D
24
Answer: 14
Explanation: 3+4=7 then 7*2=14.
Q-19
What will be printed?
Medium
Stack
Reference Code
```python
s=[1,2,3]
while s:
print(s.pop(), end=' ')
A
1 2 3
B
1 3 2
C
3 2 1
D
Error
Answer: 3 2 1
Explanation: Stack pops elements in LIFO order.
Q-20
Which of the following operations checks the top element without removing it?
Medium
Operations on Stack
A
pop
B
push
C
remove
D
peek
Answer: peek
Explanation: Peek operation returns top element without removing it.
Q-21
Evaluate postfix: 8 2 3 * +
Medium
Evaluation of Postfix Expression
A
14
B
10
C
16
D
20
Answer: 14
Explanation: 2*3=6 then 8+6=14.
Q-22
What is the postfix expression for A + B + C?
Medium
Conversion from Infix to Postfix Notation
A
A+B+C
B
AB+C+
C
ABC++
D
AB+ +C
Answer: AB+C+
Explanation: Evaluate sequentially: AB+ then add C → AB+C+.
Q-23
What will be printed?
Medium
Implementation of Stack in Python
Reference Code
```python
stack=[5]
stack.append(10)
stack.append(15)
stack.pop()
print(stack)
A
[5,10,15]
B
[5]
C
[5,10]
D
[10,15]
Answer: [5,10]
Explanation: Stack becomes [5,10,15], pop removes 15 leaving [5,10].
Q-24
Which notation places operators after operands?
Easy
Notations for Arithmetic Expressions
A
Prefix
B
Infix
C
Polish
D
Postfix
Answer: Postfix
Explanation: Postfix notation places operators after operands.
Q-25
Which operation inserts an element at the top of a stack?
Easy
Operations on Stack
A
Push
B
Pop
C
Peek
D
Display
Answer: Push
Explanation: Push operation inserts an element.
Q-26
What happens when pop() is called on an empty stack in Python list implementation?
Medium
Implementation of Stack in Python
A
Returns 0
B
Raises IndexError
C
Returns None
D
Program stops silently
Answer: Raises IndexError
Explanation: pop() on empty list raises IndexError.
Q-27
Evaluate postfix expression: 4 5 + 6 *
Medium
Evaluation of Postfix Expression
A
44
B
24
C
54
D
30
Answer: 54
Explanation: 4+5=9 then 9*6=54.
Q-28
Which data structure is primarily used to convert infix expressions to postfix?
Medium
Conversion from Infix to Postfix Notation
A
Queue
B
Tree
C
Array
D
Stack
Answer: Stack
Explanation: Stacks store operators during conversion.
Q-29
If stack = [3,6,9] and we execute stack.pop(); stack.append(12), what is the stack?
Medium
Operations on Stack
A
[3,6,12]
B
[3,6,9,12]
C
[6,9,12]
D
[3,9,12]
Answer: [3,6,12]
Explanation: pop removes 9 then append adds 12 → [3,6,12].
Q-30
Which expression represents postfix form of (A+B)*(C+D)?
Hard
Conversion from Infix to Postfix Notation
A
AB+CD+*
B
ABCD++*
C
AB+CD*+
D
AB*CD++
Answer: ABCD++*
Explanation: First (A+B) → AB+, (C+D) → CD+, multiply → AB+CD+*.
◀
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
▶