PYQ1: What data structure is used to implement recursion?
Answer: Stack
PYQ2: Which of the following is true about a stack?
a) FIFO
b) LIFO
c) Both FIFO and LIFO
d) None of these
Answer: b) LIFO
PYQ3: What will be the output of the following sequence of stack operations?
Push(5), Push(10), Pop(), Push(15), Pop(), Pop()
Answer:
Push(5) → Stack: [5]
Push(10) → Stack: [5, 10]
Pop() → Removes 10, Stack: [5]
Push(15) → Stack: [5, 15]
Pop() → Removes 15, Stack: [5]
Pop() → Removes 5, Stack: [] (empty)
PYQ4: Which operation is used to check the top element of a stack without removing it?
Answer: Peek or Top operation
PYQ5: What happens when you try to pop an element from an empty stack?
Answer: Stack Underflow error occurs.
PYQ6: Write the postfix expression for the infix expression: (A + B) * C
Answer: AB + C *
PYQ7: Explain the difference between stack and queue.
Answer:
Stack follows LIFO (Last In First Out) principle.
Queue follows FIFO (First In First Out) principle.
PYQ8: What is the output of the following postfix expression: 5 6 + 2 *?
Answer:
Evaluate 5 6 + → 11
Then 11 2 * → 22
Output: 22
PYQ9: How can a stack be used to check for balanced parentheses in an expression?
Answer:
Scan the expression from left to right.
Push every opening parenthesis '(' onto the stack.
For every closing parenthesis ')', pop from the stack and check if it matches the opening one.
If stack is empty at the end and all parentheses matched, expression is balanced.
PYQ10: What is the role of the 'top' variable in stack implementation?
Answer:
It keeps track of the index of the top element in the stack.
Helps in push and pop operations.
PYQ11: Write the infix expression for the postfix expression: ABC*+D-
Answer:
Postfix: ABC*+D-
Infix: (A + (B * C)) - D
PYQ12: What will happen if you try to push an element into a full stack?
Answer:
Stack Overflow error occurs.