Home »
MCQs
Python Data Structures MCQs (Multiple-Choice Questions)
Python provides several built-in and specialized data structures for storing and organizing collections of data. Lists, tuples, sets, and dictionaries are among the most commonly used built-in data structures, while modules such as collections and heapq provide additional specialized containers and algorithms.
Python Data Structures MCQs
These Python Data Structures MCQs cover lists, tuples, sets, dictionaries, list comprehensions, slicing, sequence unpacking, hashing, queues, stacks, deques, counters, default dictionaries, heaps, named tuples, and practical Python code.
List of Python Data Structures MCQs
Practice these multiple-choice questions to test your knowledge of Python data structures and their operations.
1. Which of the following is a mutable built-in sequence type in Python?
- tuple
- list
- str
- bytes
Answer: B) list
Explanation:
A Python list is mutable, which means its elements can be added, removed, or changed after the list is created.
2. Which syntax creates an empty list?
{}
[]
()
<>
Answer: B) []
Explanation:
Square brackets create a list. In contrast, {} creates an empty dictionary, not an empty set.
3. Which list method adds one item to the end of a list?
add()
append()
insert_end()
push()
Answer: B) append()
Explanation:
The append() method adds a single object to the end of a list.
4. What does extend() do when used with a list?
- Adds the iterable itself as one element
- Adds each item from an iterable to the list
- Sorts the list
- Removes all duplicate elements
Answer: B) Adds each item from an iterable to the list
Explanation:
extend() iterates over the supplied iterable and appends its items to the list.
5. What is the result of the following code?
numbers = [10, 20]
numbers.append([30, 40])
print(numbers)
[10, 20, 30, 40]
[10, 20, [30, 40]]
[[10, 20], 30, 40]
- TypeError
Answer: B) [10, 20, [30, 40]]
Explanation:
append() adds its argument as a single element. Therefore, the list [30, 40] becomes one nested element.
6. Which list method inserts an item at a specified position?
insert()
place()
add_at()
put()
Answer: A) insert()
Explanation:
The insert(index, value) method inserts a value before the item currently at the specified index.
7. Which statement removes and returns the last item of a list when no index is supplied?
list.remove()
list.delete()
list.pop()
list.clear()
Answer: C) list.pop()
Explanation:
pop() removes and returns an item. When no index is specified, it operates on the last item.
8. What is the difference between remove() and pop() for a list?
remove() uses an index while pop() uses a value
remove() removes by value while pop() removes by index
- Both always remove the last element
- Both only work with strings
Answer: B) remove() removes by value while pop() removes by index
Explanation:
remove(x) removes the first item equal to x, while pop(i) removes and returns the item at index i.
9. What does list.sort() return after sorting a list?
- The sorted list
- A new tuple
None
- The largest element
Answer: C) None
Explanation:
sort() sorts the list in place and returns None. The built-in sorted() function, in contrast, returns a new sorted list.
10. Which built-in function returns a new sorted list without modifying the original iterable?
sort()
sorted()
order()
arrange()
Answer: B) sorted()
Explanation:
sorted() returns a new list containing the items from the iterable in sorted order.
11. Which data structure is immutable and ordered?
- list
- set
- tuple
- dict
Answer: C) tuple
Explanation:
A tuple is an ordered sequence that cannot be modified after creation.
12. Which syntax correctly creates a one-element tuple?
(10)
[10]
(10,)
{10}
Answer: C) (10,)
Explanation:
The comma is what makes a one-element tuple. Parentheses alone do not create a tuple, so (10) is simply the integer 10.
13. What is tuple packing?
- Converting a tuple into a list
- Combining multiple values into a tuple
- Sorting tuple elements
- Removing tuple elements
Answer: B) Combining multiple values into a tuple
Explanation:
Tuple packing occurs when multiple values are combined into a tuple, such as point = 10, 20.
14. What is sequence unpacking in Python?
- Removing elements from a sequence
- Assigning elements of a sequence to multiple variables
- Sorting a sequence
- Converting a sequence to bytes
Answer: B) Assigning elements of a sequence to multiple variables
Explanation:
Sequence unpacking assigns the elements of an iterable to multiple variables, such as x, y = (10, 20).
15. What is the result of this code?
a, b, c = (10, 20, 30)
print(b)
- 10
- 20
- 30
- TypeError
Answer: B) 20
Explanation:
Sequence unpacking assigns 10 to a, 20 to b, and 30 to c.
16. Which operation extracts a portion of a sequence?
- Indexing
- Slicing
- Hashing
- Mapping
Answer: B) Slicing
Explanation:
Slicing uses the syntax sequence[start:stop:step] to obtain a portion of a sequence.
17. What is the result of [0, 1, 2, 3, 4][1:4]?
[0, 1, 2]
[1, 2, 3]
[1, 2, 3, 4]
[2, 3, 4]
Answer: B) [1, 2, 3]
Explanation:
The start index is included and the stop index is excluded. Therefore, indexes 1, 2, and 3 are included.
18. Which data structure stores unique elements and supports set operations?
- list
- tuple
- set
- dict
Answer: C) set
Explanation:
A set is a collection of distinct hashable elements and supports operations such as union, intersection, difference, and symmetric difference.
19. Which syntax creates an empty set?
{}
[]
set()
()
Answer: C) set()
Explanation:
{} creates an empty dictionary. The set() constructor is required to create an empty set.
20. What happens to duplicate values when a list is converted to a set?
- They are duplicated again
- They are retained in their original positions
- They are removed
- They are converted to None
Answer: C) They are removed
Explanation:
Sets contain unique elements. Converting an iterable containing duplicate values to a set removes the duplicate occurrences.
21. Which set operation returns elements present in both sets?
- Union
- Intersection
- Difference
- Symmetric difference
Answer: B) Intersection
Explanation:
The intersection of two sets contains elements that are common to both sets. It can be written using & or intersection().
22. Which operator represents set union?
&
|
^
-
Answer: B) |
Explanation:
The | operator computes the union of two sets, containing elements present in either set.
23. Which operator represents set intersection?
|
-
&
^
Answer: C) &
Explanation:
The & operator returns the intersection of two sets.
24. Which built-in type is used for immutable sets?
- fixedset
- frozenset
- constset
- immutableset
Answer: B) frozenset
Explanation:
frozenset is the immutable counterpart of set. Because it is immutable and hashable, it can be used where a set itself needs to be hashable.
25. Which data structure stores key-value pairs?
- list
- tuple
- dictionary
- set
Answer: C) dictionary
Explanation:
A Python dictionary is a mutable mapping that associates keys with values. Dictionary keys must satisfy the requirements for hashable keys.
26. Which syntax creates an empty dictionary?
{}
dict[]
[]
emptydict()
Answer: A) {}
Explanation:
An empty pair of curly braces creates an empty dictionary. dict() is another way to create an empty dictionary.
27. What happens when an existing dictionary key is assigned a new value?
- A duplicate key is created
- The existing value is replaced
- The dictionary becomes a set
- A TypeError is always raised
Answer: B) The existing value is replaced
Explanation:
Dictionary keys are unique. Assigning a value to an existing key replaces the value associated with that key.
28. Which dictionary method returns a view containing the dictionary's keys?
keys()
keylist()
get_keys()
items_key()
Answer: A) keys()
Explanation:
The keys() method returns a dynamic view of the dictionary's keys.
29. Which dictionary method returns key-value pairs?
pairs()
items()
entries()
values()
Answer: B) items()
Explanation:
The items() method returns a view of the dictionary's key-value pairs.
30. Which dictionary method returns the values?
values()
items()
contents()
data()
Answer: A) values()
Explanation:
The values() method returns a dynamic view of the values stored in a dictionary.
31. What does dict.get(key, default) do when the key does not exist?
- Always raises KeyError
- Returns the specified default value
- Deletes the dictionary
- Creates the key automatically in every case
Answer: B) Returns the specified default value
Explanation:
If the requested key is absent, get() returns the supplied default value. If no default is supplied, it returns None.
32. Which of the following can be used as a dictionary key?
- list
- set
- tuple containing only hashable elements
- dictionary
Answer: C) tuple containing only hashable elements
Explanation:
Dictionary keys must be hashable. A tuple is hashable when all of its elements are hashable. Lists, sets, and dictionaries are mutable and therefore cannot be dictionary keys.
33. Why can a list not normally be used as a dictionary key?
- Lists contain only numbers
- Lists are mutable and therefore unhashable
- Lists cannot contain strings
- Lists are always empty
Answer: B) Lists are mutable and therefore unhashable
Explanation:
Dictionary keys require a stable hash value. Because lists can be modified, they are unhashable and cannot be used as dictionary keys.
34. What is a list comprehension?
- A syntax for creating lists from an iterable
- A method for sorting dictionaries
- A special tuple type
- A syntax for defining classes
Answer: A) A syntax for creating lists from an iterable
Explanation:
List comprehensions provide a concise way to create lists by applying an expression to items from an iterable, optionally filtering them with a condition.
35. What is the result of the following list comprehension?
result = [x * 2 for x in range(4)]
print(result)
[1, 2, 3, 4]
[0, 2, 4, 6]
[2, 4, 6, 8]
[0, 1, 2, 3]
Answer: B) [0, 2, 4, 6]
Explanation:
range(4) produces 0, 1, 2, and 3. Each value is multiplied by 2, producing [0, 2, 4, 6].
36. Which comprehension syntax creates a set?
[x for x in data]
(x for x in data)
{x for x in data}
<x for x in data>
Answer: C) {x for x in data}
Explanation:
Set comprehensions use curly braces and produce a set containing the generated values.
37. Which comprehension syntax creates a dictionary?
{x: x * x for x in range(4)}
[x: x * x for x in range(4)]
(x: x * x for x in range(4))
<x: x * x for x in range(4)>
Answer: A) {x: x * x for x in range(4)}
Explanation:
Dictionary comprehensions use the key: value form inside curly braces.
38. Which specialized collection is designed for fast appends and pops from both ends?
collections.deque
collections.Counter
collections.ChainMap
collections.UserDict
Answer: A) collections.deque
Explanation:
deque is a double-ended queue optimized for appending and removing items from either end.
39. Which deque method removes an item from the left side?
leftpop()
popleft()
popleft()
removeleft()
Answer: B) popleft()
Explanation:
The popleft() method removes and returns the leftmost item of a deque.
40. Why is deque generally preferred over list for removing items from the left end repeatedly?
- Deque supports constant-time-style operations at both ends
- Lists cannot store strings
- Deque is immutable
- Lists do not support
append()
Answer: A) Deque supports constant-time-style operations at both ends
Explanation:
A deque is designed for efficient operations at both ends, whereas removing the first item repeatedly from a list with pop(0) requires shifting remaining elements.
41. Which class from collections is designed for counting hashable objects?
Counter
Tally
FrequencyMap
CountDict
Answer: A) Counter
Explanation:
collections.Counter is a dictionary subclass designed for counting hashable objects.
42. What is the result of Counter("banana")["a"]?
- 1
- 2
- 3
- 4
Answer: C) 3
Explanation:
The string banana contains the letter a three times, so the Counter stores a count of 3 for that character.
43. What is the main purpose of collections.defaultdict?
- To automatically supply a default value for missing keys
- To prevent all dictionary modifications
- To sort dictionary keys alphabetically
- To convert dictionaries into tuples
Answer: A) To automatically supply a default value for missing keys
Explanation:
A defaultdict uses a factory function to create a default value when a missing key is accessed.
44. What does defaultdict(list) create for a missing key?
- An empty string
- An empty list
- An empty set
- Zero
Answer: B) An empty list
Explanation:
When a missing key is accessed, defaultdict(list) calls list(), producing an empty list as the default value.
45. Which module provides functions for maintaining a heap-based priority queue?
heapq
priority
queueheap
heap
Answer: A) heapq
Explanation:
The heapq module provides an implementation of the heap queue algorithm, also known as the priority queue algorithm.
46. What value is returned by heapq.heappop() when used with a valid min-heap?
- The largest element
- The smallest element
- A random element
- The last inserted element
Answer: B) The smallest element
Explanation:
Python's heapq module implements a min-heap, so heappop() removes and returns the smallest element.
47. What is the primary purpose of collections.namedtuple()?
- To create tuple subclasses with named fields
- To create mutable dictionaries
- To implement binary heaps
- To create linked lists
Answer: A) To create tuple subclasses with named fields
Explanation:
namedtuple() creates tuple subclasses whose elements can be accessed using meaningful field names as well as indexes.
48. What is the output of the following code?
data = {"a": 10, "b": 20}
data["c"] = 30
print(list(data.keys()))
["c", "a", "b"]
["a", "b", "c"]
["a", "c", "b"]
- The order is always random
Answer: B) ["a", "b", "c"]
Explanation:
Python dictionaries preserve insertion order. The existing keys were inserted first, followed by "c".
49. What is the output of the following code?
values = [1, 2, 3, 4]
result = values[::-1]
print(result)
[1, 2, 3, 4]
[4, 3, 2, 1]
[2, 3, 4]
[4, 3, 2]
Answer: B) [4, 3, 2, 1]
Explanation:
The slice [::-1] uses a step of -1, producing the elements in reverse order.
50. What is the output of the following code?
numbers = [1, 2, 3]
alias = numbers
copy = numbers.copy()
alias.append(4)
copy.append(5)
print(numbers)
print(copy)
[1, 2, 3] and [1, 2, 3]
[1, 2, 3, 4] and [1, 2, 3, 5]
[1, 2, 3, 5] and [1, 2, 3, 4]
[1, 2, 3, 4, 5] and [1, 2, 3, 4, 5]
Answer: B) [1, 2, 3, 4] and [1, 2, 3, 5]
Explanation:
alias = numbers makes both variables refer to the same list, so appending through alias changes numbers. The copy() method creates a separate shallow copy, so appending to copy does not add 5 to numbers.
Advertisement
Advertisement