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
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
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
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]
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}
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}
47. What will be the output of the following Python code?
>>> b={}
>>> all(b)
a) { }
b) False
c) True
d) An exception is thrown
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
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’}
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’}
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
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) ‘ ‘
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
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
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) ‘ ‘
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.