×

Multiple-Choice Questions

Web Technologies MCQs

Computer Science Subjects MCQs

Databases MCQs

Programming MCQs

Testing Software MCQs

Digital Marketing Subjects MCQs

Cloud Computing Softwares MCQs

AI/ML Subjects MCQs

Engineering Subjects MCQs

Office Related Programs MCQs

Management MCQs

More

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?

  1. tuple
  2. list
  3. str
  4. 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?

  1. {}
  2. []
  3. ()
  4. <>

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?

  1. add()
  2. append()
  3. insert_end()
  4. 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?

  1. Adds the iterable itself as one element
  2. Adds each item from an iterable to the list
  3. Sorts the list
  4. 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)
  1. [10, 20, 30, 40]
  2. [10, 20, [30, 40]]
  3. [[10, 20], 30, 40]
  4. 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?

  1. insert()
  2. place()
  3. add_at()
  4. 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?

  1. list.remove()
  2. list.delete()
  3. list.pop()
  4. 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?

  1. remove() uses an index while pop() uses a value
  2. remove() removes by value while pop() removes by index
  3. Both always remove the last element
  4. 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?

  1. The sorted list
  2. A new tuple
  3. None
  4. 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?

  1. sort()
  2. sorted()
  3. order()
  4. 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?

  1. list
  2. set
  3. tuple
  4. 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?

  1. (10)
  2. [10]
  3. (10,)
  4. {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?

  1. Converting a tuple into a list
  2. Combining multiple values into a tuple
  3. Sorting tuple elements
  4. 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?

  1. Removing elements from a sequence
  2. Assigning elements of a sequence to multiple variables
  3. Sorting a sequence
  4. 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)
  1. 10
  2. 20
  3. 30
  4. 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?

  1. Indexing
  2. Slicing
  3. Hashing
  4. 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]?

  1. [0, 1, 2]
  2. [1, 2, 3]
  3. [1, 2, 3, 4]
  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?

  1. list
  2. tuple
  3. set
  4. 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?

  1. {}
  2. []
  3. set()
  4. ()

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?

  1. They are duplicated again
  2. They are retained in their original positions
  3. They are removed
  4. 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?

  1. Union
  2. Intersection
  3. Difference
  4. 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?

  1. &
  2. |
  3. ^
  4. -

Answer: B) |

Explanation:

The | operator computes the union of two sets, containing elements present in either set.

23. Which operator represents set intersection?

  1. |
  2. -
  3. &
  4. ^

Answer: C) &

Explanation:

The & operator returns the intersection of two sets.

24. Which built-in type is used for immutable sets?

  1. fixedset
  2. frozenset
  3. constset
  4. 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?

  1. list
  2. tuple
  3. dictionary
  4. 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?

  1. {}
  2. dict[]
  3. []
  4. 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?

  1. A duplicate key is created
  2. The existing value is replaced
  3. The dictionary becomes a set
  4. 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?

  1. keys()
  2. keylist()
  3. get_keys()
  4. 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?

  1. pairs()
  2. items()
  3. entries()
  4. 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?

  1. values()
  2. items()
  3. contents()
  4. 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?

  1. Always raises KeyError
  2. Returns the specified default value
  3. Deletes the dictionary
  4. 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?

  1. list
  2. set
  3. tuple containing only hashable elements
  4. 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?

  1. Lists contain only numbers
  2. Lists are mutable and therefore unhashable
  3. Lists cannot contain strings
  4. 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?

  1. A syntax for creating lists from an iterable
  2. A method for sorting dictionaries
  3. A special tuple type
  4. 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. [1, 2, 3, 4]
  2. [0, 2, 4, 6]
  3. [2, 4, 6, 8]
  4. [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?

  1. [x for x in data]
  2. (x for x in data)
  3. {x for x in data}
  4. <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?

  1. {x: x * x for x in range(4)}
  2. [x: x * x for x in range(4)]
  3. (x: x * x for x in range(4))
  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?

  1. collections.deque
  2. collections.Counter
  3. collections.ChainMap
  4. 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?

  1. leftpop()
  2. popleft()
  3. popleft()
  4. 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?

  1. Deque supports constant-time-style operations at both ends
  2. Lists cannot store strings
  3. Deque is immutable
  4. 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?

  1. Counter
  2. Tally
  3. FrequencyMap
  4. 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. 1
  2. 2
  3. 3
  4. 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?

  1. To automatically supply a default value for missing keys
  2. To prevent all dictionary modifications
  3. To sort dictionary keys alphabetically
  4. 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?

  1. An empty string
  2. An empty list
  3. An empty set
  4. 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?

  1. heapq
  2. priority
  3. queueheap
  4. 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?

  1. The largest element
  2. The smallest element
  3. A random element
  4. 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()?

  1. To create tuple subclasses with named fields
  2. To create mutable dictionaries
  3. To implement binary heaps
  4. 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()))
  1. ["c", "a", "b"]
  2. ["a", "b", "c"]
  3. ["a", "c", "b"]
  4. 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. [1, 2, 3, 4]
  2. [4, 3, 2, 1]
  3. [2, 3, 4]
  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. [1, 2, 3] and [1, 2, 3]
  2. [1, 2, 3, 4] and [1, 2, 3, 5]
  3. [1, 2, 3, 5] and [1, 2, 3, 4]
  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

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2026 www.includehelp.com. All rights reserved.