This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “Dictionary – 2”.
1. Which of these about a dictionary is false?
a) The values of a dictionary can be accessed using keys
b) The keys of a dictionary can be accessed using values
c) Dictionaries aren’t ordered
d) Dictionaries are mutable
View Answer
Explanation: In Python, you can access values by using their keys (e.g., d[key]), but the reverse is not directly possible—you cannot access keys using values unless you explicitly search through the dictionary. This makes option “The keys of a dictionary can be accessed using values” false.
2. Which of the following is not a declaration of the dictionary?
a) {1: ‘A’, 2: ‘B’}
b) dict([[1,”A”],[2,”B”]])
c) {1,”A”,2”B”}
d) { }
View Answer
Explanation: {1, “A”, 2, “B”} is not a valid dictionary declaration—it resembles a set with multiple elements, not key-value pairs. A dictionary must contain key-value pairs separated by colons. The other options are valid ways to define dictionaries.
3. What will be the output of the following Python code snippet?
a={1:"A",2:"B",3:"C"} for i,j in a.items(): print(i,j,end=" ")
a) 1 A 2 B 3 C
b) 1 2 3
c) A B C
d) 1:”A” 2:”B” 3:”C”
View Answer
Explanation: The items() method returns key-value pairs from the dictionary. The for loop unpacks each pair into i (key) and j (value), and prints them with a space between, all on the same line due to end=” “.
4. What will be the output of the following Python code snippet?
a={1:"A",2:"B",3:"C"} print(a.get(1,4))
a) 1
b) A
c) 4
d) Invalid syntax for get method
View Answer
Explanation: The get() method is used to retrieve the value for a given key. In a.get(1, 4), the key 1 exists in the dictionary, so it returns its value, which is “A”. The second argument 4 is ignored because the key is found.
5. What will be the output of the following Python code snippet?
a={1:"A",2:"B",3:"C"} print(a.get(5,4))
a) Error, invalid syntax
b) A
c) 5
d) 4
View Answer
Explanation: The get() method returns the value for the specified key if it exists; otherwise, it returns the default value provided. In this case, key 5 is not in the dictionary, so a.get(5, 4) returns the default value 4.
6. What will be the output of the following Python code snippet?
a={1:"A",2:"B",3:"C"} print(a.setdefault(3))
a) {1: ‘A’, 2: ‘B’, 3: ‘C’}
b) C
c) {1: 3, 2: 3, 3: 3}
d) No method called setdefault() exists for dictionary
View Answer
Explanation: The setdefault() method returns the value of the given key if it exists. If the key doesn’t exist, it adds the key with the specified default value. In this case, key 3 already exists in the dictionary with the value “C”, so a.setdefault(3) simply returns “C”.
7. What will be the output of the following Python code snippet?
a={1:"A",2:"B",3:"C"} a.setdefault(4,"D") print(a)
a) {1: ‘A’, 2: ‘B’, 3: ‘C’, 4: ‘D’}
b) None
c) Error
d) [1,3,6,10]
View Answer
Explanation: The setdefault() method adds a key with a specified default value if the key is not already in the dictionary. Here, key 4 is not present in a, so a.setdefault(4, “D”) adds the key-value pair 4: “D” to the dictionary. The updated dictionary is {1: ‘A’, 2: ‘B’, 3: ‘C’, 4: ‘D’}.
8. What will be the output of the following Python code?
a={1:"A",2:"B",3:"C"} b={4:"D",5:"E"} a.update(b) print(a)
a) {1: ‘A’, 2: ‘B’, 3: ‘C’}
b) Method update() doesn’t exist for dictionaries
c) {1: ‘A’, 2: ‘B’, 3: ‘C’, 4: ‘D’, 5: ‘E’}
d) {4: ‘D’, 5: ‘E’}
View Answer
Explanation: The update() method merges one dictionary into another by adding key-value pairs. Here, a.update(b) adds all items from dictionary b into a, resulting in: {1: ‘A’, 2: ‘B’, 3: ‘C’, 4: ‘D’, 5: ‘E’}.
9. What will be the output of the following Python code?
a={1:"A",2:"B",3:"C"} b=a.copy() b[2]="D" print(a)
a) Error, copy() method doesn’t exist for dictionaries
b) {1: ‘A’, 2: ‘B’, 3: ‘C’}
c) {1: ‘A’, 2: ‘D’, 3: ‘C’}
d) “None” is printed
View Answer
Explanation: The copy() method creates a shallow copy of the dictionary. When b[2] is changed to “D”, it only affects the copy b and not the original dictionary a. So, printing a shows the original unchanged dictionary {1: ‘A’, 2: ‘B’, 3: ‘C’}.
10. What will be the output of the following Python code?
a={1:"A",2:"B",3:"C"} a.clear() print(a)
a) None
b) { None:None, None:None, None:None}
c) {1:None, 2:None, 3:None}
d) { }
View Answer
Explanation: The clear() method empties the dictionary by removing all key-value pairs. After calling a.clear(), the dictionary a becomes empty, so printing it shows {}.
11. Which of the following isn’t true about dictionary keys?
a) More than one key isn’t allowed
b) Keys must be immutable
c) Keys must be integers
d) When duplicate keys encountered, the last assignment wins
View Answer
Explanation: Dictionary keys in Python must be immutable but can be of various types like strings, numbers, or tuples — they do not have to be integers specifically. So, saying “Keys must be integers” is false. The other statements are true: duplicate keys overwrite previous values, keys must be immutable, and keys must be unique.
12. What will be the output of the following Python code?
a={1:5,2:3,3:4} a.pop(3) print(a)
a) {1: 5}
b) {1: 5, 2: 3}
c) Error, syntax error for pop() method
d) {1: 5, 3: 4}
View Answer
Explanation: The pop(3) method removes the key 3 and its associated value from the dictionary a. After removal, the dictionary contains only the pairs for keys 1 and 2. So, the output is {1: 5, 2: 3}.
13. What will be the output of the following Python code?
a={1:5,2:3,3:4} print(a.pop(4,9))
a) 9
b) 3
c) Too many arguments for pop() method
d) 4
View Answer
Explanation: The pop() method removes the specified key and returns its value. If the key doesn’t exist, it returns the default value provided as the second argument. Here, key 4 is not in the dictionary, so a.pop(4, 9) returns 9.
14. What will be the output of the following Python code?
a={1:"A",2:"B",3:"C"} for i in a: print(i,end=" ")
a) 1 2 3
b) ‘A’ ‘B’ ‘C’
c) 1 ‘A’ 2 ‘B’ 3 ‘C’
d) Error, it should be: for i in a.items():
View Answer
Explanation: When you iterate over a dictionary using `for i in a`, it iterates over the **keys** by default. So the output is the keys: `1 2 3`.
15. What will be the output of the following Python code?
a={1:"A",2:"B",3:"C"} print(a.items())
a) Syntax error
b) dict_items([(‘A’), (‘B’), (‘C’)])
c) dict_items([(1,2,3)])
d) dict_items([(1, ‘A’), (2, ‘B’), (3, ‘C’)])
View Answer
Explanation: The `a.items()` method returns a view object containing the dictionary’s key-value pairs as tuples. So the output is `dict_items([(1, ‘A’), (2, ‘B’), (3, ‘C’)])`.
Sanfoundry Global Education & Learning Series – Python.
To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Check Information Technology Books
- Practice Programming MCQs
- Apply for Programming Internship
- Check Python Books
- Apply for Python Internship