Python dictionaries are a type of collection in Python. They store data in key-value pairs and are mutable, meaning you can add, modify, and delete their key-value pairs. Here we present 20 multiple-choice questions to test your knowledge of Python Dictionaries. Each MCQ has the correct answer with an explanation.
1. What is a dictionary in Python?
Answer:
Explanation:
A dictionary in Python is a collection of key-value pairs, where each key is unique and is used to access its corresponding value.
2. How do you access the value associated with the key 'color' in a dictionary 'my_dict'?
Answer:
Explanation:
The get() method is used to access the value for a given key in a dictionary. It returns None instead of an error if the key doesn't exist.
3. How do you change the value associated with the key 'age' to 30 in the dictionary 'person'?
Answer:
Explanation:
To change or add a key-value pair in a dictionary, use the syntax dictionary[key] = value.
4. How do you add a new key-value pair 'grade': 'A' to a dictionary 'student'?
Answer:
Explanation:
Adding a new key-value pair to a dictionary is done by assigning a value to a new key in the dictionary.
5. How can you remove the key 'address' from a dictionary 'details'?
Answer:
Explanation:
The del statement or the pop() method can be used to remove a key-value pair from a dictionary.
6. What does the keys() method return in a dictionary?
Answer:
Explanation:
The keys() method returns a view object that displays a list of all the keys in the dictionary.
7. What is the correct way to copy a dictionary 'original_dict' to 'new_dict'?
Answer:
Explanation:
The copy() method creates a shallow copy of the dictionary.
8. How do you create a nested dictionary representing a family tree?
Answer:
Explanation:
Nested dictionaries are created by defining a dictionary as a value within another dictionary.
9. Which method merges two dictionaries into one?
Answer:
Explanation:
The update() method adds the key-value pairs from one dictionary into another.
10. How do you retrieve all values from a dictionary 'my_dict'?
Answer:
Explanation:
The values() method returns a view object that displays a list of all the values in the dictionary.
11. How do you check if a key 'name' exists in a dictionary 'person'?
Answer:
Explanation:
The in operator is used to check if a key exists in a dictionary.
12. What is the output of len({'a': 1, 'b': 2, 'c': 3})?
Answer:
Explanation:
The len() function returns the number of key-value pairs in a dictionary.
13. How do you remove all items from a dictionary 'data'?
Answer:
Explanation:
The clear() method removes all items from a dictionary, leaving it empty.
14. What is the result of {'a': 1, 'b': 2} + {'c': 3} in Python?
Answer:
Explanation:
The + operator is not defined for dictionaries in Python.
15. What does the popitem() method do on a dictionary?
Answer:
Explanation:
The popitem() method removes and returns a (key, value) pair as a 2-tuple. Pairs are returned in LIFO order.
16. How do you get the value associated with the key 'age' or return 0 if the key does not exist?
Answer:
Explanation:
The get() method returns the value for the specified key or the second argument if the key doesn't exist.
17. What is the output of {'a': 1, 'b': 2}.get('c', 3)?
Answer:
Explanation:
Since 'c' is not a key in the dictionary, the get() method returns the default value, which is 3.
18. How do you create a dictionary from two lists – keys = ['a', 'b', 'c'] and values = [1, 2, 3]?
Answer:
Explanation:
The zip() function is used to combine two lists into a single iterator of tuples, which can be converted into a dictionary using the dict() function.
19. What does the items() method in a dictionary return?
Answer:
Explanation:
The items() method returns a view object that displays a list of dictionary's (key, value) tuple pairs.
20. How do you check if a dictionary is empty?
Answer:
Explanation:
You can check if a dictionary is empty by comparing it with an empty dictionary {} or checking if its length is zero.