MCQ On Dictionary
MCQ On Dictionary
“Dictionaries”.
1. 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
Answer: d
Explanation: Dictionaries are created by specifying keys
and values.
1. 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”)
Answer: b
3. What will be the output of the following Python code
snippet?
1. d = {"john":40, "peter":45}
2. "john" in d
a) True
b) False
c) None
d) Error
Answer: a
Explanation: In can be used to check if the key is int
dictionary.
4. What will be the output of the following Python code
snippet?
1. d1 = {"john":40, "peter":45}
2. d2 = {"john":466, "peter":45}
3. d1 == d2
a) True
b) False
c) None
d) Error
Answer: b
Explanation: If d2 was initialized as d2 = d1 the answer
would be true.
5. What will be the output of the following Python code
snippet?
1. d1 = {"john":40, "peter":45}
2. d2 = {"john":466, "peter":45}
3. d1 > d2
a) True
b) False
c) Error
d) None
Answer: c
Explanation: Arithmetic > operator cannot be used with
dictionaries.
1. d = {"john":40, "peter":45}
2. d["john"]
a) 40
b) 45
c) “john”
d) “peter”
Answer: a
Explanation: Execute in the shell to verify.
7. 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)
Answer: c
Explanation: Execute in the shell to verify.
Answer: b
Explanation: Execute in the shell to verify.
9. What will be the output of the following Python code
snippet?
1. d = {"john":40, "peter":45}
2. print(list(d.keys()))
a) [“john”, “peter”]
b) [“john”:40, “peter”:45]
c) (“john”, “peter”)
d) (“john”:40, “peter”:45)
Answer: a
Explanation: The output of the code shown above is a
list containing only keys of the dictionary d, in the form
of a list.