Python dictionaries are built-in data structures used to store data in key-value pairs, providing fast lookups, insertions, and deletions. Keys must be immutable types like strings, numbers, or tuples, while values can be of any data type and duplicated. They are mutable, meaning they can be modified in place after creation. Understanding dictionaries is essential for efficient data manipulation, handling JSON-like data structures, and optimizing search operations in Python applications.
Latest Python Dictionaries MCQs
1 min read
Correct Answer: a) {}
Explanation:
An empty dictionary is created using empty curly braces {} or the dict() constructor. Empty square brackets [] create a list, and parentheses () create a tuple.
Correct Answer: a) O(1)
Explanation:
Python dictionaries are implemented using hash tables, allowing average-case constant time complexity O(1) for lookups, insertions, and deletions.
Correct Answer: a) Tuple containing strings and integers
Explanation:
Dictionary keys must be immutable and hashable. Tuples are hashable if all their elements are hashable. Lists, dictionaries, and sets are mutable and unhashable.
Correct Answer: a) It raises a KeyError
Explanation:
Using bracket notation for a non-existent key raises a KeyError. To avoid this, the get() method can be used, which returns None by default.
Correct Answer: a) None
Explanation:
The dictionary get(key) method safely returns None if the key is absent, whereas bracket notation raises a KeyError.
Correct Answer: a) popitem()
Explanation:
popitem() removes and returns the last inserted (key, value) pair (in Python 3.7+). pop() requires a specific key argument.
Correct Answer: a) d1 | d2
Explanation:
Python 3.9 introduced the union operator (|) for dictionaries, allowing clean merging of two dictionary objects.
Correct Answer: a) It overwrites the existing value with the new value
Explanation:
The update() method updates the dictionary with elements from another dictionary or iterable of key/value pairs, overwriting existing keys.
Correct Answer: a) keys()
Explanation:
The keys() method returns a view object that displays all the keys present in the dictionary.
Correct Answer: a) A TypeError is raised because lists are unhashable
Explanation:
Lists are mutable and therefore unhashable, causing Python to raise a TypeError when attempting to use them as keys.
Correct Answer: a) It returns the current value of the key without changing it
Explanation:
If the key is in the dictionary, setdefault() returns its value. If not, it inserts the key with the specified default value and returns it.
Correct Answer: a) clear()
Explanation:
The clear() method removes all items from a dictionary, leaving it empty.
Correct Answer: a) 3
Explanation:
The len() function returns the number of key-value pairs in the dictionary, which is 3.
Correct Answer: a) dict.fromkeys(seq, value)
Explanation:
dict.fromkeys() creates a new dictionary with keys from the given iterable and values set to the specified value (defaulting to None).
Correct Answer: a) The dictionary's keys
Explanation:
Iterating over a dictionary directly loops through its keys by default.
Correct Answer: a) A view object displaying a list of a dictionary's key-value tuple pairs
Explanation:
The items() method returns a view object of (key, value) tuples.
Correct Answer: a) {k: v for k, v in iterable}
Explanation:
Dictionary comprehensions use curly braces {} with key: value format separated by a colon.
Correct Answer: a) The existing value is updated with the new value
Explanation:
Dictionary keys are unique; assigning to an existing key overwrites its current value.
Correct Answer: a) in
Explanation:
The 'in' operator checks membership against dictionary keys (e.g., 'key' in my_dict).
Correct Answer: a) It removes the specified key and returns its corresponding value
Explanation:
pop(key) removes the key and returns its value. If the key is not found, it raises a KeyError unless a default value is supplied.
Correct Answer: a) Yes, because booleans are hashable subclasses of integers
Explanation:
Booleans are hashable and immutable, and True/False can be dictionary keys (note that True evaluates to hash of 1 and False to 0).
Correct Answer: a) Dictionaries maintain insertion order
Explanation:
Since Python 3.7, standard dictionaries guarantee that insertion order is preserved.
Correct Answer: a) A view object displaying all values in the dictionary
Explanation:
The values() method returns a view object containing all the values present in the dictionary.
Correct Answer: a) d.copy()
Explanation:
The copy() method returns a shallow copy of the dictionary.
Correct Answer: a) 99
Explanation:
Since key 'z' is not in the dictionary, the get() method returns the specified default value of 99.
Correct Answer: a) del
Explanation:
The 'del' statement is used to remove an item by its key (e.g., del my_dict['key']).
Correct Answer: a) The later value overwrites the earlier value
Explanation:
Dictionary keys must be unique. If duplicate keys are specified during initialization, the last occurrence takes precedence.
Correct Answer: a) True
Explanation:
The 'in' operator checks for key presence and returns True because 'a' is a key in the dictionary.
Correct Answer: a) Yes, dictionary values can be of any data type and can be mixed
Explanation:
Dictionary values have no restrictions and can be integers, strings, lists, other dictionaries, etc., all mixed together.
Correct Answer: a) Another dictionary or an iterable of key-value pairs (like tuples)
Explanation:
The update() method can take either another dictionary object or an iterable of key/value pairs (such as a list of 2-tuples).
Correct Answer: a) {'a': 1, 'b': 2}
Explanation:
The dict() constructor accepts keyword arguments where keyword names become dictionary keys.
Correct Answer: a) They provide a dynamic view on the dictionary's entries, meaning changes to the dictionary reflect in the view
Explanation:
Dictionary views (keys(), values(), items()) are dynamic and reflect any subsequent modifications made to the dictionary.
Correct Answer: a) It raises a KeyError
Explanation:
Passing a non-existent key to pop() without a default argument raises a KeyError.
Correct Answer: a) O(n)
Explanation:
While key lookups are O(1) via hashing, value lookups require scanning all values in the dictionary, resulting in O(n) time complexity.
Correct Answer: a) pop(key, default)
Explanation:
The pop() method accepts an optional second argument as a default value returned if the key is not found.
Correct Answer: a) {0: 0, 1: 1, 2: 4}
Explanation:
Range(3) yields 0, 1, 2. The dictionary comprehension maps each number x to its square x**2.
Correct Answer: a) Yes, floats are immutable and hashable
Explanation:
Floats are immutable and hashable, making them valid dictionary keys (though using them with caution due to precision issues is recommended).
Correct Answer: a) Values from d2 overwrite values from d1 for the overlapping keys
Explanation:
d1.update(d2) merges d2 into d1, updating d1's values with d2's values where keys overlap.
Correct Answer: a) sorted(d)
Explanation:
Passing a dictionary to sorted() returns a sorted list of its keys.
Correct Answer: a) True
Explanation:
The 'in' operator checks if 1 is a key in the dictionary, returning True.
Correct Answer: a) dict_keys(['a'])
Explanation:
The keys() method returns a dict_keys view object, not a standard list.
Correct Answer: a) list(d.keys())
Explanation:
Passing the dict keys view to the list() constructor converts it into a standard list.
Correct Answer: a) It creates a dictionary where the first element of each tuple is the key and the second is the value
Explanation:
dict([('k1', 'v1'), ('k2', 'v2')]) correctly builds a dictionary from key-value pairs.
Correct Answer: a) {'person': {'name': 'Alice', 'age': 30}}
Explanation:
A nested dictionary is a dictionary in which one or more values are themselves dictionaries.
Correct Answer: a) d['person']['name']
Explanation:
Chained bracket notation is used to access values inside nested dictionaries.
Correct Answer: a) dict_items
Explanation:
items() returns a view object of type dict_items.
Correct Answer: a) |=
Explanation:
The update-or-union assignment operator (|-) updates the dictionary in-place with another dictionary's contents.
Correct Answer: a) {'a': 1, 'b': 2}
Explanation:
Dictionary unpacking using the double asterisk (**) operator merges dictionaries (pre-Python 3.9 merging technique).
Correct Answer: a) Yes, strings are immutable and hashable, making them the most common dictionary keys
Explanation:
Strings are immutable, hashable, and frequently used as keys in Python dictionaries.
Correct Answer: a) 0
Explanation:
An empty dictionary has a length of 0.
Correct Answer: a) setdefault()
Explanation:
setdefault() inserts the key with a default value if the key is not already present.
Correct Answer: a) It raises a KeyError
Explanation:
Using del on a missing key raises a KeyError.
Correct Answer: a) Set
Explanation:
Sets are mutable and unhashable, so they cannot be used as dictionary keys.
Correct Answer: a) for k, v in d.items():
Explanation:
d.items() yields (key, value) pairs, which can be unpacked directly in a for loop header.
Correct Answer: a) 0
Explanation:
Since 'c' is not present, get() returns the provided default value of 0.
Correct Answer: a) Keys must be unique
Explanation:
Dictionary keys must be unique within a dictionary; duplicate keys are not allowed.
Correct Answer: a) False
Explanation:
The 'in' operator on a dictionary checks keys, not values. To check values, you must use 'b' in d.values().
Correct Answer: a) {'a': 0, 'b': 0}
Explanation:
dict.fromkeys() assigns the specified value (0) to each key in the iterable ['a', 'b'].
Correct Answer: a) popitem()
Explanation:
popitem() removes and returns the last inserted (key, value) pair due to ordered dictionary guarantees.
Correct Answer: a) A RuntimeError is raised due to dictionary size changing during iteration
Explanation:
Modifying a dictionary's size during iteration directly triggers a RuntimeError. To modify safely, iterate over a list of keys (e.g., list(d)).
Correct Answer: a) True
Explanation:
Dictionaries compare equal if they contain the exact same key-value pairs, regardless of insertion order.
Correct Answer: a) Yes, because frozensets are immutable and hashable
Explanation:
Frozensets are immutable variants of sets, making them hashable and valid dictionary keys.
Correct Answer: a) {'a': 'a', 'b': 'b'}
Explanation:
The dictionary comprehension sets both key and value to each character from the string 'ab'.
Correct Answer: a) d[item] = d.get(item, 0) + 1
Explanation:
Using get(item, 0) safely handles missing keys by defaulting to 0 before incrementing the frequency count.
Correct Answer: a) {'a': 1, 'b': 2}
Explanation:
zip() pairs keys and values together, and dict() converts the zipped pairs into a dictionary.
Correct Answer: a) collections (OrderedDict)
Explanation:
Before standard dicts became ordered in Python 3.7, collections.OrderedDict was used to maintain insertion order.
Correct Answer: a) 0
Explanation:
defaultdict with int factory initializes missing keys with the default integer value, which is 0.
Correct Answer: a) []
Explanation:
defaultdict with list factory initializes missing keys with an empty list [] via list().
Correct Answer: a) {'a': 2}
Explanation:
Unpacking {'a': 1} and then providing 'a': 2 overrides the key 'a' with the value 2.
Correct Answer: a) Yes, integers are immutable and hashable
Explanation:
Integers are immutable and hashable, making them completely valid dictionary keys.
Correct Answer: a) Dictionaries store data as key-value pairs accessed via keys, while lists store ordered elements accessed via integer indices
Explanation:
Lists use integer indices for sequence storage, whereas dictionaries use arbitrary hashable keys for associative mapping.
Correct Answer: a) It raises a KeyError
Explanation:
Calling popitem() on an empty dictionary raises a KeyError because there are no items to remove.
Correct Answer: a) sorted(d.values())
Explanation:
sorted() takes an iterable (like d.values()) and returns a new sorted list containing those values.
Correct Answer: a) 1
Explanation:
The get() method retrieves the value associated with the specified key 'a', which is 1.
Correct Answer: a) len(my_dict)
Explanation:
The built-in len() function returns the count of key-value pairs in a dictionary.
Correct Answer: a) {2: 4, 3: 6}
Explanation:
The filter 'if x > 1' selects 2 and 3. Mapping each to x*2 yields {2: 4, 3: 6}.
Correct Answer: a) dict([1, 2, 3])
Explanation:
dict() expects an iterable of key-value pairs (like 2-tuples). Passing a plain list of integers like [1, 2, 3] raises a TypeError.
Correct Answer: a) ('b', 2)
Explanation:
popitem() removes and returns the last inserted item as a (key, value) tuple, which is ('b', 2).
Correct Answer: a) Yes, values can be duplicated across different keys
Explanation:
While dictionary keys must be unique, multiple keys can map to the exact same value.
Correct Answer: a) False
Explanation:
Since 'a' is present as a key, 'a' not in dictionary evaluates to False.
Correct Answer: a) enumerate(d.items())
Explanation:
enumerate() combined with d.items() yields an index alongside each key-value pair.
Correct Answer: a) 2
Explanation:
The get() method returns the value associated with key 'b', which is 2.
Correct Answer: a) d.clear()
Explanation:
d.clear() empties the existing dictionary in-place, whereas d = {} creates a new empty dictionary object.
Correct Answer: a) dict_keys
Explanation:
dict.keys() returns a view object of type dict_keys.
Correct Answer: a) It returns an empty dictionary {}
Explanation:
Passing an empty iterable generates no keys, resulting in an empty dictionary.
Correct Answer: a) sorted(d.items(), key=lambda item: item[1], reverse=True)
Explanation:
Sorting d.items() with a lambda key targeting element 1 (the value) and reverse=True produces a list of key-value pairs sorted by value descending.
Correct Answer: a) 5
Explanation:
Since 'b' is missing, setdefault() inserts 'b': 5 into the dictionary and returns the newly inserted value 5.
Correct Answer: a) ['a', 'b']
Explanation:
Iterating over or casting a dictionary directly to a list produces a list of its keys.
Correct Answer: a) Yes, bytes are immutable and hashable
Explanation:
Bytes are immutable sequences and are fully hashable, making them valid dictionary keys.
Correct Answer: a) O(1)
Explanation:
Clearing a dictionary resets its internal hash table pointers in constant time O(1).
Correct Answer: d) Both a and c
Explanation:
Both checking truthiness (`if not d:`) and checking length (`if len(d) == 0:`) are valid, idiomatic ways to check for an empty dictionary.
Related Posts

C++ Memory Management MCQs
Memory management in C++ empowers developers with direct control over hardware resources, primarily through the allocation and deallocation of memory…
October 20, 2026By MCQs Generator

JavaScript Operators MCQs
JavaScript operators are foundational symbols and keywords used to manipulate data, perform mathematical computations, assign values, and control programmatic flow…
October 19, 2026By MCQs Generator

Python Interview Questions MCQs
Python technical interviews evaluate a candidate’s grasp of both fundamental language features and deep runtime execution mechanics. Key topics frequently…
October 19, 2026By MCQs Generator
Related Categories

AI & Data Science MCQ
5 topics
By MCQs Generator

Arts & Humanities MCQ
4 topics
By MCQs Generator

Civil Engineering MCQ
4 topics
By MCQs Generator

Commerce & Business MCQ
4 topics
By MCQs Generator

Competitive Exams MCQ
5 topics
By MCQs Generator

Electrical & Electronics Engineering MCQ
3 topics
By MCQs Generator

General Knowledge MCQ
2 topics
By MCQs Generator

General Science MCQ
4 topics
By MCQs Generator

Law & Judiciary MCQ
3 topics
By MCQs Generator

Mechanical Engineering MCQ
4 topics
By MCQs Generator

Medical & Health Sciences MCQ
4 topics
By MCQs Generator

Modern Tech Fields MCQ
3 topics
By MCQs Generator