Python MCQs on Dictionary: SET 5

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

Check Answer

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

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

Check Answer

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

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

Check Answer

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

44. 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]

Check Answer

Answer: a
Return a new sorted list of keys in the dictionary.

45. 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}

Check Answer

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

46. 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}

Check Answer

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

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

>>> b={}

>>> all(b)

a) { }

b) False

c) True

d) An exception is thrown

Check Answer

Answer: c
Function all() returns True if all keys of the dictionary are true or if the dictionary is empty.

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

Check Answer

Answer: a
Method any() returns True if any key of the dictionary is true and False if the dictionary is empty.

49. 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’}

Check Answer

Answer: d
The above piece of code inverts the key-value pairs in the dictionary.

50. 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’}

Check Answer

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

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

Check Answer

Answer: a
The values of a dictionary can be accessed through the keys only if the keys exist in the dictionary.

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

Check Answer

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

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

Check Answer

Answer: b
The statement a=collections.defaultdict(str) gives the default value of ‘ ‘ even if the keys don’t exist in the dictionary.

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

Check Answer

Answer: d
The statement a=collections.defaultdict(lambda: x) gives the default value of x even if the keys don’t exist in the dictionary.

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

Check Answer

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

Page  1  2  3  4  5

Python Interview Questions (MCQs)

We have divided these Python Questions and Answers into various parts based on the topics. Open the Topic of your choice and Practice these MCQs.

Python MCQs on Variables and Operators Python MCQs on Precedence and Associativity
Python MCQs on Data Type Python MCQs on Boolean
Python MCQs on Bitwise Operators Python MCQs on Formatting and Advance Formatting
Python MCQs on Decorators Python MCQs on While and For Loops
Python MCQs on List Python MCQs on List Comprehension
Python MCQs on String Python MCQs on Tuple
Python MCQs on SET Python MCQs on Dictionary
Python MCQs on Functions Python MCQs on Argument Parsing
Python MCQs on Global and Local Variables Python MCQs on Recursion
Python MCQs on Mapping Functions Python MCQs on Modules
Python MCQs on Regular Expressions Python MCQs on Files
Python MCQs on Overloading Python MCQs on Classes and Objects
Python MCQs on Inheritance Python MCQs on Polymorphism and Encapsulation
Python MCQs on Exception Handling

 

Leave a Comment