Python Programming - Dictionaries

Exercise : Dictionaries - General Questions
  • Dictionaries - General Questions
16.
What does the dictionary.fromkeys(keys, default_value) method do?
Creates a new dictionary with specified keys and values.
Creates a new dictionary with specified keys and default values.
Updates an existing dictionary with new keys and values.
Removes specified keys from a dictionary.
Answer: Option
Explanation:
The fromkeys() method creates a new dictionary with specified keys and default values.

17.
What is the purpose of the dictionary.reverse() method?
Reverses the order of keys in the dictionary.
Reverses the order of values in the dictionary.
Reverses the key-value pairs in the dictionary.
There is no reverse() method for dictionaries.
Answer: Option
Explanation:
Unlike lists, dictionaries do not have a reverse() method.

18.
How do you merge two dictionaries and create a new one?
dict.merge(another_dict)
dict.join(another_dict)
{**dict, **another_dict}
dict.combine(another_dict)
Answer: Option
Explanation:
Using the syntax {**dict, **another_dict} allows you to merge two dictionaries and create a new one.

19.
How do you check if a key exists in a dictionary without raising an error?
key.exists()
key in dictionary
dictionary.has_key(key)
dictionary.key_exists(key)
Answer: Option
Explanation:
The in keyword is used to check if a key exists in a dictionary without raising an error.

20.
What does the dictionary.pop(key, default_value) method do?
Removes the key from the dictionary.
Returns the value for the key and removes it.
Returns the value for the key or the default value if the key is not present.
Adds a new key-value pair with the specified default value.
Answer: Option
Explanation:
The pop() method returns the value for the key or the default value if the key is not present and removes the key from the dictionary.