Top 15 Objective Type Questions on Data Structures in Python (With Answers)
Top 15 Objective Type Questions on Data Structures in Python
Data Structures are one of the most important topics in Python programming. Whether you are preparing for an interview, an exam, or just want to test your knowledge, these objective type questions will help you. Each question comes with an answer and explanation.
📑 Table of Contents
- 1. Mutable data structure in Python
- 2. Time complexity of list index access
- 3. LIFO data structure
- 4. Invalid list method
- 5. Preserves insertion order
- 6. set().add() usage
- 7. Key-value structure
- 8. Remove all list elements
- 9. Best queue implementation
- 10. Immutable structure
- 11. List concatenation operator
- 12. len({1,2,2,3}) output
- 13. Sorting a list
- 14. Dictionary keys method
- 15. Output of bool([])
1. Which of the following is a mutable data structure in Python?
Answer: B) List
Lists can be modified after creation, unlike tuples, strings, or frozensets.
2. What is the time complexity of accessing an element in a Python list by index?
Answer: A) O(1)
Python lists are dynamic arrays; index access is constant time.
3. Which data structure in Python follows Last In First Out (LIFO)?
Answer: B) Stack
Stacks use LIFO; lists can work as stacks with append() and pop().
4. In Python, which of the following is NOT a valid list method?
Answer: C) enqueue()
enqueue() is for queues, not Python lists.
5. Which of these preserves insertion order from Python 3.7+?
Answer: A) dict
From Python 3.7+, dicts preserve insertion order by default.
6. What does the method set().add()
do?
Answer: A) Adds one element
add() inserts a single element into a set.
7. Which of these is a key-value data structure in Python?
Answer: C) Dictionary
Dictionaries store data in key-value pairs.
8. Which method removes all elements from a list?
Answer: B) clear()
clear() empties a list completely.
9. Which data structure is best for implementing a queue in Python?
Answer: C) collections.deque
deque allows fast append/pop from both ends, perfect for queues.
10. Which of these is immutable?
Answer: C) Tuple
Tuples cannot be modified after creation.
11. Which operator is used to concatenate two lists?
Answer: B) +
The + operator joins two lists.
12. What is returned by len({1,2,2,3})
?
Answer: B) 3
Sets remove duplicates, so {1,2,2,3} → {1,2,3}.
13. Which list method sorts elements in ascending order?
Answer: A) sort()
sort() sorts the list in place; sorted() returns a new sorted list.
14. Which method is used to get all keys of a dictionary?
Answer: C) keys()
keys() returns all keys in a dictionary.
15. What is the output of bool([])
?
Answer: B) False
Empty containers evaluate to False in Python.
Post a Comment
0 Comments