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
stack = []
stack.append(10)
stack.append(20)
stack.append(30)
print(stack.pop(), stack)
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
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
stack=[1,2,3]
stack.append(4)
stack.pop()
stack.append(5)
print(stack)
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
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
s=[]
for i in range(3):
s.append(i)
print(s.pop(), s)
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
Answer: 11
Explanation: 2*3=6 → 5+6=11.
Q-7
What will be printed?
Medium
Stack
Reference Code
stack=[10,20,30]
print(stack[-1])
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
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
stack=[2,4,6]
stack.append(8)
print(len(stack))
Answer: 4
Explanation: After append(8) stack length becomes 4.
Q-10
Which data structure principle does stack follow?
Easy
Stack
Answer: LIFO
Explanation: Stack follows Last-In First-Out (LIFO).
Q-11
Evaluate postfix expression: 6 2 / 3 +
Medium
Evaluation of Postfix Expression
Answer: 6
Explanation: 6/2=3 then 3+3=6.
Q-12
What will be printed?
Medium
Operations on Stack
Reference Code
s=[1,3,5]
s.append(7)
s.append(9)
s.pop()
print(s)
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
Answer: append()
Explanation: append() adds element to top of stack.
Q-14
Which Python list operation represents POP in stack?
Easy
Operations on Stack
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
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
s=[10,20]
s.append(30)
s.append(40)
s.pop()
print(s[-1])
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
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
Answer: 14
Explanation: 3+4=7 then 7*2=14.
Q-19
What will be printed?
Medium
Stack
Reference Code
s=[1,2,3]
while s:
print(s.pop(), end=' ')
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
Answer: peek
Explanation: Peek operation returns top element without removing it.
Q-21
Evaluate postfix: 8 2 3 * +
Medium
Evaluation of Postfix Expression
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
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
stack=[5]
stack.append(10)
stack.append(15)
stack.pop()
print(stack)
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
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
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
Answer: Raises IndexError
Explanation: pop() on empty list raises IndexError.
Q-27
Evaluate postfix expression: 4 5 + 6 *
Medium
Evaluation of Postfix Expression
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
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
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
Answer: ABCD++*
Explanation: First (A+B) → AB+, (C+D) → CD+, multiply → AB+CD+*.