dictionaries: building, indexing, adding and removing keys
PCEP Certification Practice Test - Questions, Answers and Explanations
Below is a set of questions for the Certified Entry-Level Python Programmer (PCEP) examination focusing on the subtopic "dictionaries: building, indexing, adding, and removing keys." The questions use various formats, including single- and multiple-select questions, fill-in-the-gap, code fill, code insertion, sorting, and more.
Question 1: What is the correct way to create an empty dictionary in Python?
- empty_dict = []
- empty_dict = {}
- empty_dict = ()
- empty_dict = set()
Answer: B) empty_dict = {}
Explanation: In Python, an empty dictionary is created using curly braces {}.
Question 2: Which of the following are valid ways to add a key-value pair to an existing dictionary? (Choose all that apply)
- my_dict['new_key'] = 'new_value'
- my_dict.add('new_key', 'new_value')
- my_dict.update({'new_key': 'new_value'})
- my_dict.insert('new_key', 'new_value')
Answer: A) my_dict['new_key'] = 'new_value'
C) my_dict.update({'new_key': 'new_value'})
Explanation: You can add a key-value pair to a dictionary using the bracket notation (my_dict['new_key'] = 'new_value') or the update() method. The add() and insert() methods are not valid for dictionaries.
Question 3: Complete the code to create a dictionary with the following key-value pairs: 'name': 'Alice' and 'age': 30.
person = ______▼
Answer: {'name': 'Alice', 'age': 30}
Explanation: The dictionary is defined using curly braces, with keys and values separated by colons.
Question 4: What will be the output of the following code?
my_dict = {'a': 1, 'b': 2} print(my_dict['b'])
- 1
- 2
- 'b'
- KeyError
Answer: B) 2
Explanation: The code accesses the value associated with the key 'b' in my_dict, which is 2.
Question 5: Insert the correct code to remove the key 'age' from the dictionary person.
person = {'name': 'Alice', 'age': 30} ______▼
Answer: del person['age']
Explanation: The del statement is used to remove the key 'age' from the dictionary.
Question 6: What happens if you try to access a key that doesn't exist in a dictionary using square brackets, like my_dict['nonexistent_key']?
- The key is added to the dictionary with a value of None.
- A new key-value pair is added with an empty string as the value.
- The program raises a KeyError.
- The program returns None.
Answer: C) The program raises a KeyError.
Explanation: Accessing a non-existent key using square brackets will raise a KeyError.
Question 7: Which of the following statements about dictionaries are true? (Choose all that apply)
- Dictionary keys must be unique.
- Dictionary values must be unique.
- Dictionary keys can be of any immutable data type.
- Dictionary values can be of any data type.
Answer: A) Dictionary keys must be unique.
C) Dictionary keys can be of any immutable data type.
D) Dictionary values can be of any data type.
Explanation: Dictionary keys must be unique and immutable (e.g., strings, numbers, or tuples), while values can be of any data type and do not have to be unique.
Question 8: Arrange the steps to correctly remove a key-value pair from a dictionary and print the updated dictionary.
- Remove the key 'age'.
- Print the updated dictionary.
- Define the dictionary person with keys 'name' and 'age'.
Answer: 3, 1, 2
Explanation: First, define the dictionary, then remove the key 'age', and finally print the updated dictionary.
Question 9: Complete the code to retrieve the value associated with the key 'age' from the dictionary person, or return None if the key does not exist.
age = person.______('age')▼
Answer: get
Explanation: The get() method returns the value for the specified key if the key is in the dictionary; otherwise, it returns None.
Question 10: What will be the output of the following code?
my_dict = {'a': 1, 'b': 2, 'c': 3} my_dict.update({'b': 4}) print(my_dict)
- {'a': 1, 'b': 2, 'c': 3, 'b': 4}
- {'a': 1, 'b': 4, 'c': 3}
- {'a': 1, 'b': 4}
- {'b': 4}
Answer: B) {'a': 1, 'b': 4, 'c': 3}
Explanation: The update() method modifies the existing dictionary by updating the value of the key 'b' to 4.
Question 11: Insert the correct method to add a new key-value pair 'city': 'New York' to the dictionary person.
person = {'name': 'Alice', 'age': 30} person.______('city', 'New York')▼
Answer: update
Explanation: The update() method is used to add a new key-value pair to the dictionary.
Question 12: How can you safely remove a key 'age' from the dictionary person without causing an error if the key doesn't exist?
- person.remove('age')
- person.pop('age')
- person.delete('age')
- person.pop('age', None)
Answer: D) person.pop('age', None)
Explanation: The pop() method can be used with a default value (None) to safely remove a key without raising an error if the key is not present.
Question 13: Which of the following methods can be used to remove a key from a dictionary? (Choose all that apply)
- del my_dict['key']
- my_dict.pop('key')
- my_dict.remove('key')
- my_dict.popitem()
Answer: A) del my_dict['key']
B) my_dict.pop('key')
D) my_dict.popitem()
Explanation: You can remove a key using del, pop(), or popitem() (which removes the last inserted key-value pair). The remove() method does not exist for dictionaries.
Question 14: Arrange the steps to correctly update an existing key in a dictionary.
- Define a dictionary person with keys 'name' and 'age'.
- Update the value of 'age' to 31.
- Print the updated dictionary.
Answer: 1, 2, 3
Explanation: First, define the dictionary, then update the value of 'age', and finally print the updated dictionary.
Question 15: Complete the code to add a new key-value pair 'email': '[email protected]' to the dictionary person.
person = {'name': 'Alice', 'age': 30} person['email'] = ______▼
Answer: '[email protected]'
Explanation: The code adds a new key 'email' with the corresponding value '[email protected]' to the dictionary.
Question 16: What will be the output of the following code?
my_dict = {'a': 1, 'b': 2} value = my_dict.get('c', 10) print(value)
- None
- 1
- 10
- KeyError
Answer: C) 10
Explanation: The get() method returns 10 because the key 'c' is not found in my_dict, and 10 is the default value provided.
17. Insert the correct method to retrieve and remove the value associated with the key 'b' from the dictionary my_dict.
my_dict = {'a': 1, 'b': 2, 'c': 3} value = my_dict.______('b')▼
Answer: pop
Explanation: The pop() method retrieves and removes the value associated with the specified key.
Question 18: What is the result of the following code?
person = {'name': 'Alice', 'age': 30} person.clear() print(person)
- {'name': 'Alice', 'age': 30}
- {}
- None
- KeyError
Answer: B) {}
Explanation: The clear() method removes all items from the dictionary, resulting in an empty dictionary.
Question 19: Which of the following can be used as keys in a Python dictionary? (Choose all that apply)
- 'key'
- 42
- (1, 2)
- [1, 2]
Answer: A) 'key'
B) 42
C) (1, 2)
Explanation: Dictionary keys must be immutable, so strings, numbers, and tuples can be used as keys. Lists, being mutable, cannot be used as dictionary keys.
Question 20: Arrange the steps to safely retrieve a value from a dictionary or return a default value if the key does not exist.
- Use the get() method with a default value.
- Print the retrieved value.
- Define a dictionary person with some key-value pairs.
Answer: 3, 1, 2
Explanation: First, define the dictionary, then use the get() method to retrieve a value or return a default value, and finally print the result.
Question 21: Complete the code to remove the last added key-value pair from the dictionary person.
person = {'name': 'Alice', 'age': 30, 'city': 'New York'} person.______()▼
Answer: popitem
Explanation: The popitem() method removes and returns the last key-value pair from the dictionary.
Question 22: What will be the output of the following code?
my_dict = {'x': 5, 'y': 10} my_dict['z'] = 15 print(my_dict)
- {'x': 5, 'y': 10}
- {'x': 5, 'y': 10, 'z': 15}
- {'z': 15}
- {'x': 5, 'z': 15}
Answer: B) {'x': 5, 'y': 10, 'z': 15}
Explanation: The code adds the key 'z' with the value 15 to my_dict, resulting in {'x': 5, 'y': 10, 'z': 15}.
Question 23: Insert the correct code to check if the key 'age' exists in the dictionary person.
person = {'name': 'Alice', 'age': 30} if 'age' in person: print('Age found')▼
Answer: No insertion needed; the code is already correct.
Explanation: The in keyword is used to check if a key exists in a dictionary.
Question 24: What will happen if you try to add a key to a dictionary that already exists?
- The new key-value pair is added, and the old one is retained.
- A KeyError is raised.
- The old key-value pair is updated with the new value.
- The program crashes.
Answer: C) The old key-value pair is updated with the new value.
Explanation: Adding a key that already exists in the dictionary will update its value with the new one provided.
Question 25: Which of the following methods can be used to access values in a dictionary? (Choose all that apply)
- my_dict['key']
- my_dict.get('key')
- my_dict.value('key')
- my_dict['key'] = 'value'
Answer: A) my_dict['key']
B) my_dict.get('key')
Explanation: You can access values in a dictionary using square brackets ['key'] or the get() method. The value() method does not exist, and the assignment operation does not access a value but rather sets it.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/python/certificate/data-collections-dictionaries-building-indexing-adding-and-removing-keys.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics