Python Dictionary Quiz

Python Dictionary Quiz

1) What is the output of the below Python code?
x = {'a':1, 'b':2, 'c':3}
x.clear()
print(x)

2) What is the output of the below Python code?
x = {'a':1, 'b':2, 'c':3}
print(len(x))

3) Dictionaries are immutable.

4) What is the output of the below Python code?
x = {'a':1, 'b':2, 'c':3}
print(type(x))

5) Which of the following is not a dictionary?

6) What is the output of the below Python code?
x = dict((['a',1], ['b',2], ['c',3]))
print(x.keys())

7) What is the output of the below Python code?
x = {'a':1, 'b':2, 'c':3}
print(x[0])

8) Dictionaries are also known as?

9) Which of the following will create a dictionary with keys and a common value?

10) If no values are specified with the fromkeys() method, which value is assigned to the dictionary?

11) When we copy a dictionary, what will only copy the keys for the new dictionary?

12) When we copy a dictionary, what will copy keys and vales in the new dictionary?

13) What is the output of the following code?

marks = {"Bill" : 87, "Steve" : 85, "Jeff" : 81}
print("Bill" in marks)

14) What is the output of the following code?

marks = {"Bill" : 87, "Steve" : 85, "Jeff" : 81}
print(87 in marks)

15) What is the output of the following code?

dict = {"x":6, "y":20, "z":8}
s1 = ""
for i in dict:
s1 = s1+str(dict[i]) + " "
s2 = s1[:-1]
print(s2[::-1])

16) What is the output of the following code?

marks = {"Bill" : 87, "Steve" : 85, "Jeff" : 81}
marks_inv = {}
for n in marks:
marks_inv[marks[n]] = n
print(marks_inv)

17) What is the output of the following code?

t = 0
marks = {"Bill" : 87, "Steve" : 85, "Jeff" : 81, "Smith" : 85}
for i in marks:
if len(i) > 4:
t += marks[i]
print(t)

18) Which statement will result in an error?

name = "Musk"
marks = {"Bill" : 87, "Steve" : 85, "Jeff" : 81, "Smith" : 85}

#1
del marks[name]

#2
marks.pop(name, "Not Found")

19) What is the output of both print statements?

marks = {"Bill" : 87, "Steve" : 85, "Jeff" : 81, "Smith" : 85}
marks2 = marks
marks2["Jeff"] = 89
#1
print(marks["Jeff"])


marks = {"Bill" : 87, "Steve" : 85, "Jeff" : 81, "Smith" : 85}
marks2 = marks.copy()
marks2["Jeff"] = 89
#2
print(marks["Jeff"])

20) What is the output of the following code?

marks = {"Bill" : 80, "Steve" : 85, "Jeff" : 80, "Smith" : 85}
names = ["Bill", "Jeff"]
t = 0
for key, value in marks.items():
if key in names:
t = t + value

print(t)

21) Which of the following will result in an error?

#1
x = {(10,20):30, (30,40):70}
print(x[10,20])

#2
x = {[10,20]:30, [30,40]:70}
print(x[10,20])

Your score is

The average score is 61%

0%