Python Questions and Answers – Dictionary – 2

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

Answer: b
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

Answer: c
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?

advertisement
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

Answer: a
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=” “.
Free 30-Day C++ Certification Bootcamp is Live. Join Now!

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

Answer: b
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?

advertisement
a={1:"A",2:"B",3:"C"}
print(a.get(5,4))

a) Error, invalid syntax
b) A
c) 5
d) 4
View Answer

Answer: d
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

Answer: b
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

Answer: a
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

Answer: c
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

Answer: b
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

Answer: d
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

Answer: c
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

Answer: b
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

Answer: a
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

Answer: a
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

Answer: d
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.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.