Python Dictionary MCQ
This section focuses on the "Dictionary" in Python. These Multiple Choice Questions (mcq) should be practiced to improve the Python skills required for various interviews (campus interview, walk-in interview, company interview), placement, entrance exam and other competitive examinations.
1. Which of the following is correct with respect to above Python code?
d={"a":3,"b":7}
A. a dictionary d is created.
B. a and b are the keys of dictionary d.
C. 3 and 7 are the values of dictionary d
D. All of the above.
View Answer
Ans : D
Explanation: All of the above is correct with respect to above Python code.
2. Which one of the following is correct?
A. In python, a dictionary can have two same keys with different values.
B. In python, a dictionary can have two same values with different keys
C. In python, a dictionary can have two same keys or same values but cannot have two same key-value pair
D. In python, a dictionary can neither have two same keys nor two same values.
View Answer
Ans : B
Explanation: In python, a dictionary can have two same values with different keys.
3. What will be the output of above Python code?
d1={"abc":5,"def":6,"ghi":7}
print(d1[0])
A. abc
B. 5
C. {"abc":5}
D. Error
View Answer
Ans : D
Explanation: The above code will contain an error. Because 0 is not a key abc, def and ghi are keys to the dictionary.
4. What will the above Python code do?
dict={"Phy":94,"Che":70,"Bio":82,"Eng":95}
dict.update({"Che":72,"Bio":80})
A. It will create new dictionary as dict={"Che":72,"Bio":80} and old dict will be deleted.
B. It will throw an error as dictionary cannot be updated.
C. It will simply update the dictionary as dict={"Phy":94,"Che":72,"Bio":80,"Eng":95}
D. It will not throw any error but it will not do any changes in dict
View Answer
Ans : C
Explanation: It will simply update the dictionary as dict={"Phy":94,"Che":72,"Bio":80,"Eng":95}
5. What will be the result of above Python code?
dict={"Joey":1,"Rachel":2}
dict.update({"Phoebe":2})
print(dict)
A. {"Joey":1,"Rachel":2,"Phoebe":2}
B. {"Joey":1,"Rachel":2}
C. {"Joey":1,"Phoebe":2}
D. Error
View Answer
Ans : A
Explanation: It will simply print the dictionary as dict={"Joey":1,"Rachel":2,"Phoebe":2}
6. Which of the following is False regarding dictionary in Python?
dict={"Joey":1,"Rachel":2}
dict.update({"Phoebe":2})
print(dict)
A. Values of a dictionary can be string,integers or combination of both.
B. Keys of a dictionary can be string,integers or combination of both
C. The value of a dictionary can be accessed with the help of indices.
D. None of the above
View Answer
Ans : C
Explanation: The value of a dictionary can be accessed with the help of indices is False regarding dictionary in Python.
7. Which of the following will delete key_value pair for key="tiger" in dictionary?
dic={"lion":"wild","tiger":"wild","cat":"domestic","dog":"domestic"}
A. del dic["tiger"]
B. dic["tiger"].delete()
C. delete(dic.["tiger"])
D. del(dic.["tiger"])
View Answer
Ans : A
Explanation: del dic["tiger"] will delete key_value pair for key="tiger" from dictionary.
8. Which of the following will give error?
Suppose dict1={"a":1,"b":2,"c":3}
A. print(len(dict1))
B. print(dict1.get("b"))
C. dict1["a"]=5
D. None of these.
View Answer
Ans : D
Explanation: All the above statements are true and do not give any errors.
9. Which of the following Python codes will give same output if
(i) dict.pop("book")
(ii) del dict["book"]
(iii) dict.update({"diary":1,"novel":5})
dict={"diary":1,"book":3,"novel":5}
A. i, ii, iii
B. i, ii
C. i, iii
D. ii, iii
View Answer
Ans : B
Explanation: del dict["book"] and dict.pop("book") are thefollowing Python codes will give same output.
10. What will be the following Python code?
dict1={"a":10,"b":2,"c":3}
str1=""
for i in dict1:
str1=str1+str(dict1[i])+" "
str2=str1[:-1]
print(str2[::-1])
A. 3, 2
B. 3, 2, 10
C. 3, 2, 01
D. Error
View Answer
Ans : C
Explanation: 3, 2, 01 will be the following Python code output.
Discussion