250+ TOP MCQs on Python Dictionary and Answers

Python Multiple Choice Questions on “Dictionary – 4”.

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
Answer: b
Clarification: The output of the code is: {‘test’:7}.

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) 25
b) 17
c) 16
d) Tuples can’t be made keys of a dictionary
Answer: c
Clarification: 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.

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) 3
c) 2
d) An exception is thrown
Answer: b
Clarification: Now, a={1:[2,3,4],2:1} . a[1][1] refers to second element having key 1.

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]
Answer: a
Clarification: Return a new sorted list of keys in the dictionary.

5. What will be the output of the following Python code?

>>> a={i: i*i for i in range(6)}
>>> a

a) Dictionary comprehension doesn’t exist
b) {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6:36}
c) {0: 0, 1: 1, 4: 4, 9: 9, 16: 16, 25: 25}
d) {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
Answer: d
Clarification: Dictionary comprehension is implemented in the above piece of code.

  250+ TOP MCQs on Python Inheritance and Answers

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}
Answer: b
Clarification: 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.

7. What will be the output of the following Python code?

a) { }
b) False
c) True
d) An exception is thrown
Answer: c
Clarification: Function all() returns True if all keys of the dictionary are true or if the dictionary is empty.

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 dictionary is empty
c) Returns True if all keys of the dictionary are true
d) Method any() doesn’t exist for dictionary
Answer: a
Clarification: Method any() returns True if any key of the dictionary is true and False if the dictionary is empty.

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) An exception is thrown
c) {‘a’: ‘b’: ‘c’: }
d) {1: ‘a’, 2: ‘b’, 3: ‘c’}
Answer: d
Clarification: The above piece of code inverts the key-value pairs in the dictionary.

10. What will be the output of the following Python code?

>>> a={i: 'A' + str(i) for i in range(5)}
>>> a

a) An exception is thrown
b) {0: ‘A0’, 1: ‘A1’, 2: ‘A2’, 3: ‘A3’, 4: ‘A4’}
c) {0: ‘A’, 1: ‘A’, 2: ‘A’, 3: ‘A’, 4: ‘A’}
d) {0: ‘0’, 1: ‘1’, 2: ‘2’, 3: ‘3’, 4: ‘4’}

Answer: b
Clarification: Dictionary comprehension and string concatenation is implemented in the above piece of code.

11. What will be the output of the following Python code?

a) An exception is thrown since the dictionary is empty
b) ‘ ‘
c) 1
d) 0
Answer: a
Clarification: The values of a dictionary can be accessed through the keys only if the keys exist in the dictionary.

12. What will be the output of the following Python code?

>>> import collections
>>> a=dict()
>>> a=collections.defaultdict(int)
>>> a[1]

a) 1
b) 0
c) An exception is thrown
d) ‘ ‘
Answer: b
Clarification: 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.

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
Answer: b
Clarification: The statement a=collections.defaultdict(str) gives the default value of ‘ ‘ even if the keys don’t exist in the dictionary.

14. What will be the output of the following Python code?

>>> import collections
>>> b=dict()
>>> b=collections.defaultdict(lambda: 7)
>>> b[4]

a) 4
b) 0
c) An exception is thrown
d) 7
Answer: d
Clarification: The statement a=collections.defaultdict(lambda: x) gives the default value of x even if the keys don’t exist in the dictionary.

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) ‘ ‘

Answer: b
Clarification: The line of code a=collections.OrderedDict() generates a dictionary satisfying the conditions given within the parenthesis and in an ascending order of the keys.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top