Python Multiple Choice Questions & Answers (MCQs) focuses on “Dictionary – 4”.
Q 1. The following Python code is invalid.
class demo(dict):
def __test__(self,key):
return []
a = demo()
a[‘test’] = 7
print(a)
A. True
B. False
Show Answer
Answer:-B. FalseExplanation
The output of the code is: {‘test’:7}.
Q 2. What will be the output of the following Python code?
count={}
count[(1,2,4)] = 5
count[(4,2,1)] = 7
count[(1,2)] = 6
count[(4,2,1)] = 2
tot = 0
for i in count:
tot=tot+count[i]
print(len(count)+tot)
A. 16
B. 17
C. 25
D. Tuples can’t be made keys of a dictionary
Show Answer
Answer:-A. 16Explanation
Tuples can be made keys of a dictionary. Length of the dictionary is 3 as the value of the key (4,2,1) is modified to 2. The value of the variable tot is 5+6+2=13.Q 3. What will be the output of the following Python code?
a={}
a[2]=1
a[1]=[2,3,4]
print(a[1][1])
A. [2,3,4]
B. 2
C. 3
D. An exception is thrown
Show Answer
Answer:-C. 3Explanation
Now, a={1:[2,3,4],2:1} . a[1][1] refers to second element having key 1.
Q 4. What will be the output of the following Python code?
>>> a={‘B’:5,’A’:9,’C’:7}
>>> sorted(a)
A. [‘A’,’B’,’C’]
B. [‘B’,’C’,’A’]
C. [5,7,9]
D. [9,5,7]
Show Answer
Answer:-A. [‘A’,’B’,’C’]Explanation
Return a new sorted list of keys in the dictionary.
Q 5. What will be the output of the following Python code?
>>> a={i: i*i for i in range(6)}
>>> a
A. {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6:36}
B. {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
C. {0: 0, 1: 1, 4: 4, 9: 9, 16: 16, 25: 25}
D. Dictionary comprehension doesn’t exist
Show Answer
Answer:-B. {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}Explanation
Dictionary comprehension is implemented in the above piece of code.
Q 6. What will be the output of the following Python code?
>>> a={}
>>> a.fromkeys([1,2,3],”check”)
A. Syntax error
B. {1:”check”,2:”check”,3:”check”}
C. “check”
D. {1:None,2:None,3:None}
Show Answer
Answer:-B. {1:”check”,2:”check”,3:”check”}Explanation
The dictionary takes values of keys from the list and initializes it to the default value (value given in the second parameter). Execute in Python shell to verify.
Q 7. What will be the output of the following Python code?
>>> b={}
>>> all(b)
A. { }
B. False
C. True
D. An exception is thrown
Show Answer
Answer:-C. TrueExplanation
Function all() returns True if all keys of the dictionary are true or if the dictionary is empty.
Q 8. If b is a dictionary, what does any(b) do?
A. Returns True if any key of the dictionary is true
B. Returns False if any key of the dictionary is false
C. Returns True if all keys of the dictionary are true
D. Method any() doesn’t exist for dictionary
Show Answer
Answer:-A. Returns True if any key of the dictionary is trueExplanation
Method any() returns True if any key of the dictionary is true and False if the dictionary is empty. It also returns False if all keys are false.
Q 9. What will be the output of the following Python code?
>>> a={“a”:1,”b”:2,”c”:3}
>>> b=dict(zip(a.values(),a.keys()))
>>> b
A. {‘a’: 1, ‘b’: 2, ‘c’: 3}
B. {1: ‘a’, 2: ‘b’, 3: ‘c’}
C. {‘a’: ‘b’: ‘c’: }
D. An exception is thrown
Show Answer
Answer:-B. {1: ‘a’, 2: ‘b’, 3: ‘c’}Explanation
The above piece of code inverts the key-value pairs in the dictionary.
Q 10. What will be the output of the following Python code?
>>> a={i: ‘A’ + str(i) for i in range(5)}
>>> a
A. {0: ‘0’, 1: ‘1’, 2: ‘2’, 3: ‘3’, 4: ‘4’}
B. {0: ‘A0’, 1: ‘A1’, 2: ‘A2’, 3: ‘A3’, 4: ‘A4’}
C. {0: ‘A’, 1: ‘A’, 2: ‘A’, 3: ‘A’, 4: ‘A’}
D. An exception is thrown
Show Answer
Answer:-B. {0: ‘A0’, 1: ‘A1’, 2: ‘A2’, 3: ‘A3’, 4: ‘A4’}Explanation
Dictionary comprehension and string concatenation is implemented in the above piece of code.
Q 11. What will be the output of the following Python code?
>>> a=dict()
>>> a[1]
A. An exception is thrown since the dictionary is empty
B. ‘ ‘
C. 1
D. 0
Show Answer
Answer:-A. An exception is thrown since the dictionary is emptyExplanation
The values of a dictionary can be accessed through the keys only if the keys exist in the dictionary.
Q 12. What will be the output of the following Python code?
>>> import collections
>>> a=dict()
>>> a=collections.defaultdict(int)
>>> a[1]
A. 0
B. 1
C. An exception is thrown
D. ‘ ‘
Show Answer
Answer:-A. 0Explanation
The statement a=collections.defaultdict(int) gives the default value of 0 (since int data type is given within the parenthesis) even if the keys don’t exist in the dictionary.
Q 13. What will be the output of the following Python code?
>>> import collections
>>> a=dict()
>>> a=collections.defaultdict(str)
>>> a[‘A’]
A. An exception is thrown since the dictionary is empty
B. ‘ ‘
C. ‘A’
D. 0
Show Answer
Answer:-B. ‘ ‘Explanation
The statement a=collections.defaultdict(str) gives the default value of ‘ ‘ even if the keys don’t exist in the dictionary.
Q 14. What will be the output of the following Python code?
>>> import collections
>>> b=dict()
>>> b=collections.defaultdict(lambda: 7)
>>> b[4]
A. 0
B. 4
C. 7
D. An exception is thrown
Show Answer
Answer:-C. 7Explanation
The statement a=collections.defaultdict(lambda: x) gives the default value of x even if the keys don’t exist in the dictionary.
Q 15. What will be the output of the following Python code?
>>> import collections
>>> a=collections.OrderedDict((str(x),x) for x in range(3))
>>> a
A. {‘2’:2, ‘0’:0, ‘1’:1}
B. OrderedDict([(‘0’, 0), (‘1’, 1), (‘2’, 2)])
C. An exception is thrown
D. ‘ ‘
Leave a Reply