Python Programming - Dictionaries

Exercise : Dictionaries - General Questions
  • Dictionaries - General Questions
6.
How do you merge two dictionaries?
dict.merge(another_dict)
dict.join(another_dict)
dict + another_dict
dict.update(another_dict)
Answer: Option
Explanation:
The update() method is used to merge two dictionaries in Python.

7.
What is the purpose of the dictionary.items() method?
Returns a list of all keys.
Returns a list of all values.
Returns a list of key-value pairs.
Returns a list of default values.
Answer: Option
Explanation:
The items() method returns a list of key-value pairs in the dictionary.

8.
How do you create a dictionary with a default value for all new keys?
dictionary.set_default(default_value)
dictionary.default_value = default_value
dictionary.default(default_value)
collections.defaultdict(default_value)
Answer: Option
Explanation:
The defaultdict from the collections module is used to create a dictionary with a default value for all new keys.

9.
What is the result of the expression dictionary.get('key', 'default')?
Raises a KeyError if 'key' is not present.
Returns the value of 'key' if present, otherwise returns 'default'.
Returns 'default' if 'key' is not present.
Returns a tuple containing 'key' and its value.
Answer: Option
Explanation:
The get() method returns the value for 'key' if present, otherwise returns the specified default value.

10.
How do you remove all items from a dictionary?
dictionary.delete_all()
dictionary.clear()
dictionary.remove_all()
dictionary.erase()
Answer: Option
Explanation:
The clear() method removes all items from a dictionary.