# Python Questions and Answers – Dictionary – 4

This set of Python Multiple Choice Questions & Answers (MCQs) focuses 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
View Answer

Answer: b
Explanation: The output of the code is: {‘test’:7}.
advertisement
advertisement

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
View Answer

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

advertisement
```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
View Answer

Answer: b
Explanation: Now, a={1:[2,3,4],2:1} . a[1][1] refers to second element having key 1.
advertisement

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]
View Answer

Answer: a
Explanation: 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}
View Answer

Answer: d
Explanation: Dictionary comprehension is implemented in the above piece of code.

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}
View Answer

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

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

```>>> b={}
>>> all(b)```

a) { }
b) False
c) True
d) An exception is thrown
View Answer

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

Answer: a
Explanation: 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’}
View Answer

Answer: d
Explanation: 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’}
View Answer

Answer: b
Explanation: 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=dict()
>>> a[1]```

a) An exception is thrown since the dictionary is empty
b) ‘ ‘
c) 1
d) 0
View Answer

Answer: a
Explanation: 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) ‘ ‘
View Answer

Answer: b
Explanation: 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
View 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.

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
View Answer

Answer: d
Explanation: 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) ‘ ‘
View Answer

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

Sanfoundry Global Education & Learning Series – Python.

To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

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, 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.