Python Multiple Choice Questions & Answers (MCQs) focuses on “Dictionary – 2”.
Q 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
Show Answer
Answer:-B. The keys of a dictionary can be accessed using valuesExplanation
The values of a dictionary can be accessed using keys but the keys of a dictionary can’t be accessed using values.
Q 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. { }
Show Answer
Answer:-C. {1,”A”,2”B”}Explanation
Option c is a set, not a dictionary.
Q 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”
Show Answer
Answer:-A. 1 A 2 B 3 CExplanation
In the above code, variables i and j iterate over the keys and values of the dictionary respectively.Q 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
Show Answer
Answer:-B. AExplanation
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.
Q 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. 4
B. A
C. 5
D. Error, invalid syntax
Show Answer
Answer:-A. 4Explanation
The get() method returns the default value(second parameter) if the key isn’t present in the dictionary.
Q 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
Show Answer
Answer:-B. CExplanation
setdefault() is similar to get() but will set dict[key]=default if key is not already in the dictionary.
Q 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. [1,3,6,10]
C. Error
D. None
Show Answer
Answer:-A. {1: ‘A’, 2: ‘B’, 3: ‘C’, 4: ‘D’}Explanation
setdefault() will set dict[key]=default if key is not already in the dictionary.
Q 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. {1: ‘A’, 2: ‘B’, 3: ‘C’, 4: ‘D’, 5: ‘E’}
C. {4: ‘D’, 5: ‘E’}
D. Method update() doesn’t exist for dictionaries
Show Answer
Answer:-B. {1: ‘A’, 2: ‘B’, 3: ‘C’, 4: ‘D’, 5: ‘E’}Explanation
update() method adds dictionary b’s key-value pairs to dictionary a. Execute in python shell to verify.
Q 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. {1: ‘A’, 2: ‘B’, 3: ‘C’}
B . {1: ‘A’, 2: ‘D’, 3: ‘C’}
C. Error, copy() method doesn’t exist for dictionaries
D. “None” is printed
Show Answer
Answer:-A. {1: ‘A’, 2: ‘B’, 3: ‘C’}Explanation
Changes made in the copy of the dictionary isn’t reflected in the original one.
Q 10. What will be the output of the following Python code?
a={1:”A”,2:”B”,3:”C”}
a.clear()
print(a)
A. { }
B. { None:None, None:None, None:None}
C. {1:None, 2:None, 3:None}
D. None
Show Answer
Answer:-A. { }Explanation
The clear() method clears all the key-value pairs in the dictionary.
Q 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
Show Answer
Answer:-C. Keys must be integersExplanation
Keys of a dictionary may be any data type that is immutable.
Q 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. {1: 5, 3: 4}
D. Error, syntax error for pop() method
Show Answer
Answer:-B. {1: 5, 2: 3}Explanation
pop() method removes the key-value pair for the key mentioned in the pop() method.
Q 13. What will be the output of the following Python code?
a={1:5,2:3,3:4}
print(a.pop(4,9))
A. 3
B. 4
C. 9
D. Too many arguments for pop() method
Show Answer
Answer:-C. 9Explanation
pop() method returns the value when the key is passed as an argument and otherwise returns the default value(second argument) if the key isn’t present in the dictionary.
Q 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():
Show Answer
Answer:-A. 1 2 3Explanation
The variable i iterates over the keys of the dictionary and hence the keys are printed.
Q 15. What will be the output of the following Python code?
>>> a={1:”A”,2:”B”,3:”C”}
>>> 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’)])
Leave a Reply