Test Series - python

Test Number 56/108

Q: Which of the following statements create a dictionary?
A. d = {}
B. d = {“john”:40, “peter”:45}
C. d = {40:”john”, 45:”peter”}
D. All of the mentioned
Solution: Dictionaries are created by specifying keys and values.
Q: What will be the output of the following Python code snippet?

d = {"john":40, "peter":45}
A. “john”, 40, 45, and “peter”
B. “john” and “peter”
C. 40 and 45
D. d = (40:”john”, 45:”peter”)
Solution: Dictionaries appear in the form of keys and values.
Q: What will be the output of the following Python code snippet?

d = {"john":40, "peter":45}
"john" in d
A. True
B. False
C. None
D. Error
Solution: In can be used to check if the key is int dictionary.
Q: What will be the output of the following Python code snippet?

d1 = {"john":40, "peter":45}
d2 = {"john":466, "peter":45}
d1 == d2
A. True
B. False
C. None
D. Error
Solution: If d2 was initialized as d2 = d1 the answer would be true.
Q: What will be the output of the following Python code snippet?

d1 = {"john":40, "peter":45}
d2 = {"john":466, "peter":45}
d1 > d2
A. True
B. False
C. Error
D. None
Solution: Arithmetic > operator cannot be used with dictionaries.
Q: What will be the output of the following Python code snippet?

d = {"john":40, "peter":45}
d["john"]
A. 40
B. 45
C. “john”
D. “peter”
Solution: Execute in the shell to verify.
Q: Suppose d = {“john”:40, “peter”:45}, to delete the entry for “john” what command do we use?
A. d.delete(“john”:40)
B. d.delete(“john”)
C. del d[“john”]
D. del d(“john”:40)
Solution: Execute in the shell to verify.
Q: Suppose d = {“john”:40, “peter”:45}. To obtain the number of entries in dictionary which command do we use?
A. d.size()
B. len(d)
C. size(d)
D. d.len()
Solution: Execute in the shell to verify.
Q: What will be the output of the following Python code snippet?

d = {"john":40, "peter":45}
print(list(d.keys()))
A. [“john”, “peter”]
B. [“john”:40, “peter”:45]
C. (“john”, “peter”)
D. (“john”:40, “peter”:45)
Solution: The output of the code shown above is a list containing only keys of the dictionary d, in the form of a list.
Q: Suppose d = {“john”:40, “peter”:45}, what happens when we try to retrieve a value using the expression d[“susan”]?
A. Since “susan” is not a value in the set, Python raises a KeyError exception
B. It is executed fine and no exception is raised, and it returns None
C. Since “susan” is not a key in the set, Python raises a KeyError exception
D. Since “susan” is not a key in the set, Python raises a syntax error
Solution: Execute in the shell to verify.

You Have Score    /10