Python Programming - Dictionaries

Exercise : Dictionaries - General Questions
  • Dictionaries - General Questions
21.
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.

22.
What is the result of the expression len(dictionary.values())?
Returns the number of keys in the dictionary.
Returns the number of values in the dictionary.
Returns the total number of key-value pairs.
Raises a TypeError.
Answer: Option
Explanation:
The len() function returns the number of values in a dictionary.

23.
How do you remove a key-value pair from a dictionary in Python without raising an error if the key is not present?
dictionary.remove_key(key)
dictionary.delete(key)
dictionary.remove(key)
dictionary.pop(key, None)
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.

24.
How do you create an empty dictionary?
empty_dict = {}
empty_dict = dict.empty()
empty_dict = new Dictionary()
empty_dict = dict()
Answer: Option
Explanation:
An empty dictionary in Python can be created using curly braces {}.

25.
What is the purpose of the dictionary.update(another_dict) method?
Merges the two dictionaries, updating values for common keys.
Adds a new key-value pair to the dictionary.
Removes a key-value pair from the dictionary.
Creates a new dictionary by combining keys and values.
Answer: Option
Explanation:
The update() method merges two dictionaries, updating values for common keys.