CARVIEW |
Python Dictionary Quiz
Question 1
What will be the output of the following code?
s = "GeeksforGeeks"
print(s[0], s[-1])
G G
G s
G k
e k
Question 2
Find the output of the following program:
d = dict()
for x in enumerate(range(2)):
d[x[0]] = x[1]
d[x[1]+7] = x[0]
print(d)
{0: 1, 7: 0, 1: 1, 8: 0}
{1: 1, 7: 2, 0: 1, 8: 1}
{0: 0, 7: 0, 1: 1, 8: 1}
KeyError
Question 3
Find the output of the following program:
d1 = {'GFG' : 1,
'Google' : 2,
'GFG' : 3
}
print(d1['GFG']);
Compilation error due to duplicate keys
Runtime time error due to duplicate keys
3
1
Question 4
Find the output of the following program:
d = {}
def addTodict(country):
if country in d:
d[country] += 1
else:
d[country] = 1
addTodict('China')
addTodict('Japan')
addTodict('china')
print (len(d))
0
1
2
3
Question 5
Find the output of the following program:
d = {1:'1', 2:'2', 3:'3'}
del d[1]
d[1] = '10'
del d[2]
print (len(d) )
1
2
3
4
Question 6
Find the output of the following program:
a ={}
a['a']= 1
a['b']=[2, 3, 4]
print(a)
{‘b’: [2], ‘a’: 1}
{'a': 1, 'b': [2, 3, 4]}
{‘b’: [2], ‘a’: [3]}
Error
Question 7
Find the output of the following program:
d = {1:'A', 2:'B', 3:'C'}
del d[1]
d[1] = 'D'
del d[2]
print(len(d))
Error
0
1
2
Question 8
Find the output of the following program:
a = {}
a[1] = 1
a['1'] = 2
a[1]= a[1]+1
count = 0
for i in a:
count += a[i]
print(count)
2
4
1
Error
Question 9
Find the output of the following program:
d ={1:"geek", 2:"for", 3:"geeks"}
del d
del deletes the entire dictionary
del doesn’t exist for the dictionary
del deletes the keys in the dictionary
del deletes the values in the dictionary
Question 10
Find the output of the following program:
d = {"geek":10, "for":45, "geeks": 90}
print("geek" in d)
10
False
True
Error
There are 24 questions to complete.