Python Programming - Dictionaries
Exercise : Dictionaries - General Questions
- Dictionaries - General Questions
26.
What does the
dictionary.clear()
method do?
Answer: Option
Explanation:
The
clear()
method removes all items from a dictionary.
27.
What is the result of the expression
dictionary.get('key')
?
Answer: Option
Explanation:
The
get()
method returns the value for 'key' if present, otherwise returns None
.
28.
How do you remove a key-value pair from a dictionary in Python without raising an error if the key is not present?
Answer: Option
Explanation:
The
pop()
method with a default value of None
removes a key-value pair from a dictionary without raising an error if the key is not present.
29.
What is the purpose of the
dictionary.copy()
method?
Answer: Option
Explanation:
The
copy()
method creates a shallow copy of the dictionary.
30.
How do you check if a key exists in a dictionary and, if not, add a default value to it?
Answer: Option
Explanation:
The provided code checks if the key exists in the dictionary, and if not, adds the key with the specified default value.
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers