Python Dictionary MCQ : Set 1

Python Dictionary MCQ

1). What is a dictionary in Python?

a) An ordered collection of elements
b) A sequence of key-value pairs
c) A data structure for storing integers
d) A built-in function in Python

Corresct answer is: b) A sequence of key-value pairs
Explanation: A dictionary in Python is an unordered collection of key-value pairs, where each key is unique.

2). Which symbol is used to define a dictionary in Python?

a) []
b) ()
c) {}
d) //

Corresct answer is: c) {}
Explanation: Curly braces ({}) are used to define a dictionary in Python.

3). What is the output of the following code?

my_dict = {‘name’: ‘Ketan’, ‘age’: 29}
print(my_dict[‘name’])

a) ‘name’
b) ‘John’
c) {‘name’: ‘John’}
d) 25

Corresct answer is: b) ‘Ketan’
Explanation: The code accesses the value associated with the key ‘name’ in the dictionary and prints it, which is ‘John’.

4). Can a dictionary contain duplicate keys?

a) Yes
b) No

Corresct answer is: b) No
Explanation: Dictionary keys must be unique. If a duplicate key is encountered during assignment, the last value assigned to that key will be retained.

5). How do you add a new key-value pair to a dictionary?

a) Using the append() method
b) Using the add() method
c) Using the insert() method
d) By assigning a value to a new key

Corresct answer is: d) By assigning a value to a new key
Explanation: To add a new key-value pair to a dictionary, you can simply assign a value to a new key using the assignment operator (=).

6). What happens when you access a key that does not exist in a dictionary?

a) An error is raised
b) None is returned
c) An empty string is returned
d) The dictionary expands to include the new key

Corresct answer is: a) An error is raised
Explanation: Accessing a key that does not exist in a dictionary raises a KeyError.

7). Which of the following methods in Python is used to remove a key-value pair from a dictionary?

a) delete()
b) remove()
c) discard()
d) del

Corresct answer is: d) del
Explanation: The `del` keyword is used to remove a key-value pair from a dictionary by specifying the key to be deleted.

8). What is the purpose of the keys() method in a dictionary?

a) To retrieve all the keys in a dictionary
b) To retrieve all the values in a dictionary
c) To retrieve both the keys and values in a dictionary
d) To sort the dictionary keys in ascending order

Corresct answer is: a) To retrieve all the keys in a dictionary
Explanation: The keys() method returns a view object that contains all the keys in the dictionary.

9). Which method returns a list of all the values in a dictionary?

a) values()
b) items()
c) get()
d) keys()

Corresct answer is: a) values()
Explanation: The values() method returns a view object that contains all the values in the dictionary.

10). Which method can be used to iterate over both the keys and values in a dictionary?

a) items()
b) keys()
c) values()
d) iterate()

Corresct answer is: a) items()
Explanation: The items() method returns a view object that contains key-value pairs as tuples, allowing iteration over both keys and values.

11). Which method returns the value associated with a specified key in a dictionary?

a) get()
b) value()
c) fetch()
d) retrieve()

Corresct answer is: a) get()
Explanation: The get() method returns the value associated with a specified key in a dictionary. If the key does not exist, it can return a default value.

12). Which method can be used to check if a key exists in a dictionary?

a) has_key()
b) contains()
c) in
d) exists()

Corresct answer is: c) in
Explanation: The `in` operator can be used to check if a key exists in a dictionary. It returns a Boolean value (True or False).

13). Which method is used to clear all the key-value pairs from a dictionary?

a) clear()
b) delete()
c) remove()
d) empty()

Corresct answer is: a) clear()
Explanation: The clear() method removes all the key-value pairs from a dictionary, making it an empty dictionary.

14). Which method is used to update a dictionary with the key-value pairs from another dictionary?

a) merge()
b) update()
c) add()
d) append()

Corresct answer is: b) update()
Explanation: The update() method is used to update a dictionary with the key-value pairs from another dictionary.

15). Which method returns a shallow copy of a dictionary?

a) copy()
b) clone()
c) duplicate()
d) replicate()

Corresct answer is: a) copy()
Explanation: The copy() method returns a shallow copy of a dictionary, creating a new dictionary with the same key-value pairs.

16). Which of the following is an immutable data type and can be used as a key in a dictionary?

a) List
b) Tuple
c) Set
d) Dictionary

Corresct answer is: b) Tuple
Explanation: Tuples are immutable data types and can be used as keys in a dictionary. Lists, sets, and dictionaries are mutable and cannot be used as keys.

17). Which method returns a new dictionary with keys and values swapped?

a) switch()
b) swap()
c) reverse()
d) reversed()

Corresct answer is: c) reverse()
Explanation: The reverse() method returns a new dictionary where the keys and values are swapped.

18). Which method can be used to create a dictionary from two lists, one containing keys and the other containing values?

a) dict()
b) create()
c) make()
d) generate()

Corresct answer is: a) dict()
Explanation: The dict() function can be used to create a dictionary from two lists, where one list contains keys and the other contains values.

19). Which method returns the minimum key in a dictionary?

a) min_key()
b) smallest_key()
c) min()
d) min_key()

Corresct answer is: c) min()
Explanation: The min() method returns the minimum key in a dictionary based on their natural ordering.

20). Which method returns the maximum value in a dictionary?

a) max_value()
b) largest_value()
c) max()
d) max_value()

Corresct answer is: c) max()
Explanation: The max() method returns the maximum value in a dictionary based on their natural ordering.

21). Which method is used to sort the keys in a dictionary in ascending order?

a) sort()
b) sort_keys()
c) sorted()
d) order()

Corresct answer is: b) sort_keys()
Explanation: The sort_keys() method is used to sort the keys in a dictionary in ascending order.

22). Which method is used to sort the values in a dictionary in ascending order?

a) sort_values()
b) sort()
c) sorted()
d) order_values()

Corresct answer is: a) sort_values()
Explanation: The sort_values() method is used to sort the values in a dictionary in ascending order.

23). Which method is used to reverse the order of the keys in a dictionary?

a) reverse_keys()
b) reverse()
c) reversed()
d) invert_keys()

Corresct answer is: c) reversed()
Explanation: The reversed() method returns a reversed view object for the keys in a dictionary.

24). Which method can be used to perform a shallow comparison between two dictionaries?

a) compare()
b) equals()
c) compare_dicts()
d) __eq__()

Corresct answer is: d) __eq__()
Explanation: The __eq__() method can be used to perform a shallow comparison between two dictionaries for equality.

25). Which method returns a hash value for a dictionary?

a) hash()
b) get_hash()
c) calculate_hash()
d) __hash__()

Corresct answer is: d) __hash__()
Explanation: The __hash__() method returns a hash value for a dictionary, allowing it to be used as a key in another dictionary or a member of a set.

Leave a Comment