Python Dictionary Quiz

Last Updated :
Discuss
Comments

Question 1

What will be the output of the following code?

Python
d = {'a': 5, 'b': 10}
d['a'] = 15
print(d)
  • {'a': 5, 'b': 10}

  • {'a': 15, 'b': 10}

  • Error

  • {'a': 15}

Question 2

Find the output of the following program:

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

Python
d1 = {'GFG' : 1, 'Google' : 2, 'GFG1' : 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:

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

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

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

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

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

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

Python
d = {"geek":10, "for":45, "geeks": 90} 
print("geek" in d) 
  • 10

  • False

  • True

  • Error

There are 24 questions to complete.

Take a part in the ongoing discussion