Dictionary Questions In Python: Python Language MCQs

Dictionary Questions In Python. Multiple Choice Questions related to Dictionary in Python.

 

Dictionary Questions In Python

 

 

What will be the output of the following Python code snippet ??

a={}
a[‘a’]=1
a[‘b’]=[2,3,4]
print(a)

a) Exception is thrown
b) {‘b’: [2], ‘a’: 1}
c) {‘b’: [2], ‘a’: [3]}
d) {‘b’: [2, 3, 4], ‘a’: 1}

 

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

 

What will be the output of the following Python code snippet ??

a = {}
a[1] = 1
a[‘1’] = 2
a[1.0]=4
count = 0
for i in a:
count += a[i]
print(count)

a) An exception is thrown
b) 3
c) 6
d) 2

 

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

 

Dictionary Questions In Python

 

What will be the output of the following Python code snippet ??

test = {1:’A’, 2:’B’, 3:’C’}
del test[1]
test[1] = ‘D’
del test[2]
print(len(test))

a) 0
b) 2
c) Error as the key-value pair of 1:’A’ is already deleted
d) 1

 

What will be the output of the following Python ??

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

 

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

test = {1:’A’, 2:’B’, 3:’C’}
test = {}
print(len(test))

a) 0
b) None
c) 3
d) An exception is thrown

 

What will be the output of the code ??

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

a) 1
b) 0
c) An exception is thrown
d) ‘ ‘

 

Dictionary Questions In Python

 

What will be the output of the following Python code snippet ??

numbers = {}
letters = {}
comb = {}
numbers[1] = 56
numbers[3] = 7
letters[4] = ‘B’
comb[‘Numbers’] = numbers
comb[‘Letters’] = letters
print(comb)

a) Error, dictionary in a dictionary can’t exist
b) ‘Numbers’: {1: 56, 3: 7}
c) {‘Numbers’: {1: 56}, ‘Letters’: {4: ‘B’}}
d) {‘Numbers’: {1: 56, 3: 7}, ‘Letters’: {4: ‘B’}}

 

What will be the output of the following Python ??

>>> a=dict()
>>> a[1]

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

 

What will be the output of the following Python code snippet ??

a = {}
a[1] = 1
a[‘1’] = 2
a[1]=a[1]+1
count = 0
for i in a:
count += a[i]
print(count)

a) 1
b) 2
c) 4
d) Error, the keys can’t be a mixture of letters and numbers

 

What will be the output ??

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

 

Dictionary Questions In Python

 

What will be the output of the following Python ??

total={}
def insert(items):
if items in total:
total[items] += 1
else:
total[items] = 1
insert(‘Apple’)
insert(‘Ball’)
insert(‘Apple’)
print (len(total))

a) 3
b) 1
c) 2
d) 0

 

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

 

If a is a dictionary with some key-value pairs, what does a.popitem() do ??

a) Removes an arbitrary element
b) Removes all the key-value pairs
c) Removes the key-value pair for the key given as an argument
d) Invalid method for dictionary

Leave a Comment

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

error: Content is protected !!