Python Multiple Choice Questions 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
Answer: b
Clarification: The values of a dictionary can be accessed using keys but the keys of a dictionary can’t be accessed using values.
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) { }
Answer: c
Clarification: Option c is a set, not a dictionary.
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”
Answer: a
Clarification: In the above code, variables i and j iterate over the keys and values of the dictionary respectively.
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
Answer: b
Clarification: The get() method returns the value of the key if the key is present in the dictionary and the default value(second parameter) if the key isn’t present in the dictionary.
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
Answer: d
Clarification: The get() method returns the default value(second parameter) if the key isn’t present in the dictionary.
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
Answer: b
Clarification: setdefault() is similar to get() but will set dict[key]=default if key is not already in the dictionary.
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]
Answer: a
Clarification: setdefault() will set dict[key]=default if key is not already in the dictionary.
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’}
Answer: c
Clarification: update() method adds dictionary b’s key-value pairs to dictionary a. Execute in python shell to verify.
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
Answer: b
Clarification: Changes made in the copy of the dictionary isn’t reflected in the original one.
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) { }
Answer: d
Clarification: The clear() method clears all the key-value pairs in the dictionary.
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
