PUC II STATE BOARD / CBSE XII COMPUTER SCIENCE - Chapter 3 Stacks in Python, collection of questions



 Complete set of questions for chapter on Stacks. Questions are categorized them into MCQs, 1-mark, 2-mark, 3-mark, and 5-mark questions with answers.


50 MCQs with Answers

1.    Stack follows which principle?
a) FIFO
b) LIFO
c) FILO
d) None of these
Answer: b) LIFO

2.    Which Python data type is commonly used to implement a stack?
a) Tuple
b) List
c) Dictionary
d) Set
Answer: b) List

3.    Which operation removes an element from the top of the stack?
a) PUSH
b) POP
c) APPEND
d) REMOVE
Answer: b) POP

4.    Which operation adds an element to the top of the stack?
a) POP
b) PUSH
c) APPEND
d) INSERT
Answer: b) PUSH

5.    What happens if we try to pop an element from an empty stack?
a) Overflow
b) Underflow
c) Error-free
d) None
Answer: b) Underflow

6.    Which of the following is a real-life example of a stack?
a) Queue in bank
b) Pile of plates
c) Railway coach
d) ATM line
Answer: b) Pile of plates

7.    Which of the following is true about append() in Python?
a) Deletes element from stack
b) Adds element to end of list
c) Pops element from stack
d) Checks stack size
Answer: b) Adds element to end of list

8.    What is the output of this code?

stack=[]

stack.append(5)

stack.append(10)

print(stack.pop())

a) 5
b) 10
c) 0
d) Error
Answer: b) 10

9.    Which of these is not an application of stack?
a) Undo/Redo in editor
b) Web browser history
c) Queue at ticket counter
d) Expression evaluation
Answer: c) Queue at ticket counter

10.                       Topmost element in a stack is referred as?
a) Bottom
b) Top
c) End
d) Start
Answer: b) Top

11.                       Which function in Python returns the number of elements in a stack?
a) count()
b) size()
c) len()
d) getSize()
Answer: c) len()

12.                       Postfix expression is also called:
a) Prefix notation
b) Infix notation
c) Reverse Polish notation
d) BODMAS notation
Answer: c) Reverse Polish notation

13.                       In prefix notation, operators are placed:
a) Between operands
b) After operands
c) Before operands
d) At random
Answer: c) Before operands

14.                       Which algorithm is used to convert infix to postfix expression?
a) BFS
b) DFS
c) Stack-based algorithm
d) Queue-based algorithm
Answer: c) Stack-based algorithm

15.                       While evaluating postfix expression, which element is pushed onto stack?
a) Operators
b) Operands
c) Both
d) None
Answer: b) Operands

16.                       Which symbol is used in Python for comments?
a) //
b) #
c) /* */
d) %
Answer: b) #

17.                       Which method is used to remove an element from a list in Python?
a) remove()
b) pop()
c) delete()
d) discard()
Answer: b) pop()

18.                       Stack is a type of:
a) Non-linear data structure
b) Linear data structure
c) Hierarchical structure
d) Graph
Answer: b) Linear data structure

19.                       Which of these is not a valid stack operation?
a) PUSH
b) POP
c) TOP
d) SEARCH
Answer: d) SEARCH

20.                       In a stack, where is a new element inserted?
a) Bottom
b) Top
c) Random position
d) None
Answer: b) Top

21.                       What is the result of postfix evaluation: 7 8 + 2 *?
a) 30
b) 15
c) 20
d) 25
Answer: a) 30

22.                       If a stack has elements [1,2,3], the top element is:
a) 1
b) 2
c) 3
d) None
Answer: c) 3

23.                       Which built-in function adds an element at the end of a list?
a) add()
b) insert()
c) append()
d) push()
Answer: c) append()

24.                       Underflow occurs when:
a) Stack is empty and we POP
b) Stack is full and we PUSH
c) Stack is empty and we PUSH
d) Stack has one element
Answer: a) Stack is empty and we POP

25.                       Which Python data type is immutable?
a) List
b) String
c) Set
d) Dictionary
Answer: b) String

26.                       Parentheses are mandatory in which notation?
a) Infix
b) Prefix
c) Postfix
d) None
Answer: a) Infix

27.                       To evaluate a postfix expression, we require:
a) Queue
b) Stack
c) List
d) Dictionary
Answer: b) Stack

28.                       Which of the following is a correct postfix equivalent of (A+B)*C?
a) AB+C*
b) AB+C
c) ABC
+
d) +ABC
Answer: a) AB+C

29.                       Which of the following is a correct prefix equivalent of (A+B)*C?
a) +ABC
b) AB+C

c) A+BC
d) +A
BC
Answer: a) *+ABC

30.                       What will be printed by:

stack=[]

print(stack.pop())

a) None 

b) Underflow 

c) Error 

d) 0 

**Answer:** c) Error

31. In Python, list pop() removes element from:
a) Beginning by default
b) End by default
c) Random position
d) Cannot remove
Answer: b) End by default

32.                       Which real-life scenario does NOT use stack?
a) Undo in editor
b) Plate stack
c) Queue at ticket counter
d) Browsing history
Answer: c) Queue at ticket counter

33.                       Stack is also called:
a) Queue
b) LIFO list
c) Array
d) Graph
Answer: b) LIFO list

34.                       Which operator has the highest precedence in x + y * z?
a) +
b) *
c) -
d) /
Answer: b) *

35.                       In Python, len(stack) returns:
a) Top element
b) Size of stack
c) Empty stack
d) Last element
Answer: b) Size of stack

36.                       While converting infix to postfix, which elements are pushed to stack?
a) Operands
b) Operators
c) Both
d) None
Answer: b) Operators

37.                       Algorithm to evaluate postfix expression pops how many elements for a binary operator?
a) One
b) Two
c) Three
d) None
Answer: b) Two

38.                       Which of the following is NOT a valid arithmetic expression notation?
a) Infix
b) Postfix
c) Prefix
d) Reverse Infix
Answer: d) Reverse Infix

39.                       Python list append() is analogous to which stack operation?
a) POP
b) PUSH
c) TOP
d) ISEMPTY
Answer: b) PUSH

40.                       To check if a stack is empty in Python, we can use:
a) if len(stack)==0
b) if stack==None
c) if top==None
d) if stack.pop()==None
Answer: a) if len(stack)==0

41.                       The main advantage of postfix expressions is:
a) Easy for humans to read
b) No need for parentheses
c) Operators are ignored
d) Only addition is possible
Answer: b) No need for parentheses

42.                       Which is the first element popped in a LIFO stack?
a) Bottom
b) Top
c) Random
d) None
Answer: b) Top

43.                       In Python, stack overflow occurs when:
a) List reaches memory limit
b) Pop on empty stack
c) Append on empty list
d) List is None
Answer: a) List reaches memory limit

44.                       The postfix of ((2+3)*(4/2))+2 is:
a) 2 3 + 4 2 / * 2 +
b) + * + 2 3 / 4 2 2
c) 2 3 4 2 / * + 2
d) 2+3*4/2+2
Answer: a) 2 3 + 4 2 / * 2 +

45.                       If stack is [5,6,7], after stack.pop(), stack becomes:
a) [5,6]
b) [5,6,7]
c) [6,7]
d) [7]
Answer: a) [5,6]

46.                       Which of the following is TRUE?
a) Stack supports insertion at both ends
b) Stack supports insertion at one end only
c) Stack supports deletion from bottom only
d) Stack is non-linear
Answer: b) Stack supports insertion at one end only

47.                       Which Python function returns last element of stack without removing?
a) top()
b) pop()
c) append()
d) last()
Answer: a) top()

48.                       Undo operation in text editors uses:
a) Queue
b) Stack
c) List
d) Set
Answer: b) Stack

49.                       Which notation requires operator precedence rules for evaluation?
a) Prefix
b) Postfix
c) Infix
d) All of these
Answer: c) Infix

50.                       Which data structure is used to check for balanced parentheses?
a) Queue
b) Stack
c) List
d) Set
Answer: b) Stack

Great 👍 Here’s the complete set of 1-mark, 2-mark, 3-mark, and 5-mark questions with answers (continuing from the 50 MCQs already done):


📘 STACK – Questions & Answers


🔹 10 Questions – 1 Mark Each

1.    Define a stack.
👉 A stack is a linear data structure that follows LIFO (Last In, First Out) principle.

2.    What is the difference between push and pop operation?
👉 Push adds an element to stack, pop removes the top element.

3.    Which error occurs when we delete an element from an empty stack?
👉 Underflow.

4.    Which function is used in Python to insert an element in stack?
👉 append()

5.    Which function removes element from stack in Python?
👉 pop()

6.    Write an example of real-life stack.
👉 Stack of plates/books.

7.    Which data structure is used to implement recursion?
👉 Stack.

8.    What does the top() function return?
👉 The top element of the stack.

9.    Which expression format requires parentheses?
👉 Infix.

10.                       What is the output of postfix expression 5 6 +?
👉 11


🔹 10 Questions – 2 Marks Each

1.    Differentiate between stack and queue.
👉 Stack → LIFO (Last In First Out)
👉 Queue → FIFO (First In First Out)

2.    What is stack underflow? Give example.
👉 Removing element from empty stack.
Ex:
pop() on empty list.

3.    What is the main advantage of postfix expression over infix?
👉 Postfix does not need parentheses to determine precedence.

4.    Explain role of stack in undo/redo feature.
👉 Each action is pushed to stack; undo pops last action; redo re-pushes it.

5.    Write Python code to create empty stack.

stack = []

6.    What will be the stack after following operations?
Push 10, Push 20, Pop.
👉 Stack = [10]

7.    What is the result of prefix + 3 * 2 4?
👉 3 + (2*4) = 11

8.    Which stack operation is performed first in recursion?
👉 Push of function call.

9.    What is stack overflow?
👉 Trying to push element in full stack (or when memory is full).

10.                       Write postfix for infix (A+B)*C.
👉 AB+C*


🔹 10 Questions – 3 Marks Each

1.    Explain three applications of stack.
👉 (i) Undo/Redo in editors
👉 (ii) Function calls/Recursion
👉 (iii) Expression conversion & evaluation

2.    Convert infix to postfix: (A+B)/(C-D).
👉 Postfix = AB+CD-/

3.    Evaluate postfix expression: 6 2 / 3 * 4 +.
👉 (6/2) = 3 → 3*3 = 9 → 9+4 = 13

4.    Write Python code to implement stack with push operation.

stack = []
stack.append(5)
stack.append(10)
print(stack)

5.    Explain difference between infix, prefix, and postfix.
👉 Infix: A + B
👉 Prefix: +AB
👉 Postfix: AB+

6.    Write algorithm for push operation.
👉 Step 1: Check for overflow
👉 Step 2: Increase top pointer
👉 Step 3: Insert element at top

7.    Explain postfix evaluation process briefly.
👉 Operands pushed into stack → Operator pops 2 operands → Apply operator → Push result back → Continue till end.

8.    What will stack contain after sequence? Push A, Push B, Pop, Push C.
👉 Stack = [A, C]

9.    Convert to prefix: (A-B)*(C+D).
👉 Prefix = * -AB +CD

10.                       Differentiate between overflow and underflow with diagram.
👉 Overflow: push into full stack.
👉 Underflow: pop from empty stack.


🔹 10 Questions – 5 Marks Each (Elaborated Answers)

1.    Explain stack operations PUSH and POP with algorithm and example.
👉 PUSH inserts element at top; POP removes from top.
Example: Stack [10,20]. PUSH(30) → [10,20,30]. POP() → [10,20].
Algorithms:

·         PUSH: Check overflow → Increment top → Insert.

·         POP: Check underflow → Return top → Decrement top.


2.    Write Python program to implement stack with menu-driven options: push, pop, display.

stack = []
 
def push(x):
    stack.append(x)
 
def pop():
    if not stack:
        return "Underflow"
    return stack.pop()
 
def display():
    return stack
 
# Example usage
push(10)
push(20)
print(display())   # [10, 20]
print(pop())       # 20
print(display())   # [10]

3.    Explain infix, prefix, postfix with example conversion.
👉 Infix: Operator between operands (A+B).
👉 Prefix: Operator before operands (+AB).
👉 Postfix: Operator after operands (AB+).
Example: Infix
(A+B)*C → Prefix *+ABC → Postfix AB+C*.


4.    Convert the following infix to postfix and evaluate: ((3+4)*2)-5.
👉 Infix: ((3+4)2)-5
👉 Postfix: 34+2*5-
👉 Evaluation: (3+4)=7 → 72=14 → 14-5= 9


5.    Explain applications of stack in real life and computer science.
👉 Real life: stack of plates, undo/redo in text editor.
👉 Computer science: recursion, expression evaluation, syntax parsing, backtracking, memory management.


6.    Trace evaluation of postfix expression 7 2 3 * - 4 +.
👉 Step 1: 2*3 = 6 → [7,6]
👉 Step 2: 7-6 = 1 → [1]
👉 Step 3: 1+4 = 5 → Final = 5


7.    Write algorithm for converting infix to postfix.
👉 1. Initialize empty stack for operators.
👉 2. Scan infix expression left to right.
👉 3. If operand → add to postfix.
👉 4. If operator → push into stack (check precedence).
👉 5. If ‘)’ → pop until ‘(’.
👉 6. At end → pop all remaining operators.


8.    Explain working of recursion using stack.
👉 Every function call is pushed into stack; execution continues until base case; then functions are popped in reverse order. Example: factorial(3) → factorial(2) → factorial(1).


9.    Convert (A+B)*(C-D)/E to postfix and prefix.
👉 Postfix = AB+CD-*E/
👉 Prefix = / * +AB -CD E


10.                       Discuss the importance of stack in compiler design.
👉 Stack used for syntax parsing, function call management, memory allocation, expression evaluation, and matching parentheses. Without stack, compiler cannot handle nested structures.

 

 

Comments