Python Stack MCQs | 25 Practice Questions with Answers | CBSE Class 12 Computer Science
Class 12 · Computer Science
Python Stack MCQs (10 Questions)
Practice these multiple-choice questions to strengthen your understanding of the Stack data structure. Click Show Answer to reveal the correct answer and explanation.
Q1. What is a stack?
A. A linear data structure that follows FIFO.
B. A linear data structure that follows LIFO.
C. A tree data structure.
D. A graph data structure.
Show Answer
Answer: B. A linear data structure that follows LIFO.
Explanation: A stack follows the Last In, First Out (LIFO) principle, where the last inserted element is removed first.
Q2. What does LIFO stand for?
A. Last In, First Out
B. Last In, Final Out
C. Large Input File Output
D. Long Input First Output
Show Answer
Answer: A. Last In, First Out
Explanation: In a stack, the most recently inserted element is removed first.
Q3. Which operation is used to insert an element into a stack?
A. insert
B. append
C. push
D. add
Show Answer
Answer: C. push
Explanation: The push operation inserts an element at the top of the stack.
Q4. Which operation removes the top element from a stack?
A. delete
B. remove
C. pop
D. clear
Show Answer
Answer: C. pop
Explanation: The pop operation removes and returns the topmost element from the stack.
Q5. Which Python data structure is commonly used to implement a stack?
A. Dictionary
B. Tuple
C. List
D. Set
Show Answer
Answer: C. List
Explanation: Python lists provide the append() and pop() methods, making them suitable for implementing a stack.
Q6. Which list method is commonly used to perform the push operation?
A. insert()
B. add()
C. append()
D. extend()
Show Answer
Answer: C. append()
Explanation: The append() method adds an element to the end of the list, which represents the top of the stack.
Q7. Which list method is commonly used to perform the pop operation?
A. remove()
B. delete()
C. pop()
D. clear()
Show Answer
Answer: C. pop()
Explanation: The pop() method removes the last element of the list, which represents the top of the stack.
Q8. In a stack, from where is an element inserted?
A. Bottom only
B. Middle only
C. Top only
D. Anywhere
Show Answer
Answer: C. Top only.
Explanation: All insertions in a stack take place at the top.
Q9. In a stack, from where is an element removed?
A. Bottom only
B. Middle only
C. Top only
D. Anywhere
Show Answer
Answer: C. Top only.
Explanation: The top element is always removed first in a stack.
Q10. Which real-life example best represents a stack?
A. Queue at a ticket counter
B. Stack of plates in a cafeteria
C. Railway reservation system
D. Student attendance register
Show Answer
Answer: B. Stack of plates in a cafeteria.
Explanation: Plates are placed and removed from the top, following the LIFO principle.
Q11. What will be the output of the following code?
stack=[]
stack.append(10)
stack.append(20)
print(stack)
A. [10]
B. [20,10]
C. [10,20]
D. Error
Show Answer
Answer: C. [10,20]
Explanation: The append() method adds elements to the end of the list, representing the top of the stack.
Q12. What will be the output of the following code?
stack=[10,20,30]
print(stack.pop())
A. 10
B. 20
C. 30
D. Error
Show Answer
Answer: C. 30
Explanation: The pop() method removes and returns the last element of the list.
Q13. What will be the contents of the stack after executing the following code?
stack=[5,10]
stack.append(15)
stack.pop()
A. [5]
B. [5,10]
C. [10,15]
D. [5,15]
Show Answer
Answer: B. [5,10]
Explanation: The value 15 is pushed and immediately removed using pop().
Q14. Which operation is performed first in a stack?
A. The first inserted element is removed first.
B. The last inserted element is removed first.
C. Elements are removed randomly.
D. Elements are removed alphabetically.
Show Answer
Answer: B.
Explanation: A stack follows the Last In, First Out (LIFO) principle.
Q15. Which operation checks whether a stack is empty?
A. len(stack)==0
B. stack.empty()
C. isempty(stack)
D. stack.size()
Show Answer
Answer: A.
Explanation: In Python, a stack implemented using a list is considered empty when its length is zero.
Q16. What will be the output of the following code?
stack=[]
stack.append(50)
stack.append(60)
print(stack.pop())
A. 50
B. 60
C. [50,60]
D. Error
Show Answer
Answer: B. 60
Explanation: The last inserted element (60) is removed first.
Q17. Which method is used to insert an element at the top of a stack implemented using a list?
A. insert()
B. append()
C. add()
D. extend()
Show Answer
Answer: B. append()
Explanation: The append() method adds an element at the end of the list, which acts as the top of the stack.
Q18. Which method removes the topmost element from a stack implemented using a list?
A. remove()
B. delete()
C. pop()
D. clear()
Show Answer
Answer: C. pop()
Explanation: The pop() method removes and returns the last element of the list.
Q19. What happens if pop() is called on an empty stack?
A. The program returns 0.
B. Nothing happens.
C. An IndexError is raised.
D. The stack is automatically created.
Show Answer
Answer: C.
Explanation: Calling pop() on an empty list raises an IndexError.
Q20. Which statement is true about stack implementation using a Python list?
A. append() performs the push operation.
B. pop() performs the pop operation.
C. The end of the list represents the top of the stack.
D. All of these.
Show Answer
Answer: D. All of these.
Explanation: A Python list can efficiently implement a stack using append() for push and pop() for pop, with the end of the list acting as the top.
Q21. Which operation is performed when an element is inserted into a stack?
A. Enqueue
B. Push
C. Pop
D. Dequeue
Show Answer
Answer: B. Push
Explanation: The push operation inserts a new element at the top of the stack.
Q22. Which operation is performed when an element is removed from a stack?
A. Push
B. Insert
C. Pop
D. Append
Show Answer
Answer: C. Pop
Explanation: The pop operation removes and returns the topmost element of the stack.
Q23. What will be the output of the following code?
stack=[100]
stack.append(200)
stack.append(300)
stack.pop()
print(stack)
A. [100]
B. [100,200]
C. [200,300]
D. [100,300]
Show Answer
Answer: B. [100,200]
Explanation: The element 300 is pushed last and removed first using pop(), leaving [100,200].
Q24. Which of the following applications commonly uses a stack?
A. Browser Back button
B. Printer queue
C. Railway reservation system
D. ATM transaction queue
Show Answer
Answer: A. Browser Back button
Explanation: Browsers use a stack to store visited pages. The most recently visited page is accessed first when the Back button is pressed.
Q25. Which statement about a stack is correct?
A. It follows the FIFO principle.
B. Insertion and deletion take place only at the top.
C. Elements can be inserted and deleted from both ends.
D. It stores only integer values.
Show Answer
Answer: B. Insertion and deletion take place only at the top.
Explanation: A stack follows the LIFO principle, and both insertion (push) and deletion (pop) are performed only at the top of the stack.
Answer Key
| Q.No. | Answer | Q.No. | Answer | Q.No. | Answer |
|---|---|---|---|---|---|
| 1 | B | 10 | B | 19 | C |
| 2 | A | 11 | C | 20 | D |
| 3 | C | 12 | C | 21 | B |
| 4 | C | 13 | B | 22 | C |
| 5 | C | 14 | B | 23 | B |
| 6 | C | 15 | A | 24 | A |
| 7 | C | 16 | B | 25 | B |
| 8 | C | 17 | B | ||
| 9 | C | 18 | C |
Practice Tips
- Remember that a Stack follows the LIFO (Last In, First Out) principle.
- Insertion into a stack is called Push, while deletion is called Pop.
- In Python, a stack is commonly implemented using a list.
- Use
append()to perform the push operation. - Use
pop()to perform the pop operation. - Before performing a pop operation, check whether the stack is empty using
len(stack)==0to avoid anIndexError. - Revise real-life applications of stacks such as:
- Browser Back button
- Undo operation in text editors
- Function call management
- Expression evaluation
- Practice output-based questions involving push and pop operations, as they are frequently asked in CBSE Class 12 Computer Science examinations.