PCEP Certification Practice: Dictionary Methods keys(), items(), and values()
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 methods: keys(), items(), and values()." 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 does the keys() method return when called on a dictionary?
- A list of all the values in the dictionary.
- A view object of all the keys in the dictionary.
- A list of key-value pairs in the dictionary.
- A copy of the dictionary.
Answer: B) A view object of all the keys in the dictionary.
Explanation: The keys() method returns a view object containing all the keys in the dictionary.
Question 2: Which of the following statements correctly describe the items() method in a dictionary? (Choose all that apply)
- It returns a list of all the keys in the dictionary.
- It returns a view object of key-value pairs.
- Each element in the view object returned is a tuple.
- It allows both keys and values to be accessed simultaneously.
Answer: B) It returns a view object of key-value pairs.
C) Each element in the view object returned is a tuple.
D) It allows both keys and values to be accessed simultaneously.
Explanation: The items() method returns a view object containing tuples, where each tuple is a key-value pair from the dictionary.
Question 3: Complete the code to retrieve a view object of all the values in the dictionary my_dict.
my_dict = {'a': 1, 'b': 2, 'c': 3} values = my_dict.______()▼
Answer: values
Explanation: The values() method returns a view object containing all the values in the dictionary.
Question 4: What will be the output of the following code?
my_dict = {'x': 10, 'y': 20, 'z': 30} print(list(my_dict.keys()))
- ['x', 'y', 'z']
- [(10, 20, 30)]
- [10, 20, 30]
- [('x', 10), ('y', 20), ('z', 30)]
Answer: A) ['x', 'y', 'z']
Explanation: The keys() method returns a view object of all the keys in the dictionary. Converting it to a list results in ['x', 'y', 'z'].
Question 5: Insert the correct method to print all key-value pairs of the dictionary person as tuples.
person = {'name': 'Alice', 'age': 30} for item in person.______(): print(item)▼
Answer: items
Explanation: The items() method returns key-value pairs as tuples, which can be iterated over in the loop.
Question 6: What does the following code output?
info = {'course': 'Python', 'level': 'beginner'} for value in info.values(): print(value)
- Python beginner
- course level
- ('course', 'Python') ('level', 'beginner')
- None
Answer: A) Python beginner
Explanation: The values() method returns a view object containing all the values in the dictionary, which are then printed.
Question 7: Which of the following methods can be used to iterate through a dictionary to access both its keys and values? (Choose all that apply)
- my_dict.keys()
- my_dict.values()
- my_dict.items()
- for key, value in my_dict.items():
Answer: C) my_dict.items()
D) for key, value in my_dict.items():
Explanation: The items() method and its usage in a loop allow you to access both keys and values simultaneously.
Question 8: Arrange the steps to correctly retrieve and print all keys from the dictionary data.
- Convert the view object to a list.
- Use the keys() method to get the keys.
- Print the list of keys.
Answer: 2, 1, 3
Explanation: First, use the keys() method to get the keys, convert the view object to a list, and then print the list.
Question 9: Complete the code to get a list of all key-value pairs in the dictionary settings.
settings = {'theme': 'dark', 'volume': 'medium'} pairs = list(settings.______) print(pairs)▼
Answer: items()
Explanation: The items() method returns a view object of key-value pairs, which is then converted to a list.
Question 10: What will be the output of the following code?
fruits = {'apple': 3, 'banana': 5, 'cherry': 7} for fruit in fruits.keys(): print(fruit)
- 3 5 7
- apple banana cherry
- apple: 3 banana: 5 cherry: 7
- None
Answer: B) apple banana cherry
Explanation: The keys() method returns a view object of all the keys in the dictionary, which are then printed in the loop.
Question 11: Insert the correct method to print only the values from the dictionary inventory.
inventory = {'pens': 10, 'notebooks': 5, 'erasers': 15} print(list(inventory.______()))▼
Answer: values
Explanation: The values() method returns a view object of all the values in the dictionary, which can be converted to a list and printed.
Question 12: What is the result of the following code?
contact = {'email': '[email protected]', 'phone': '123-456-7890'} print(contact.items())
- ['email', '[email protected]', 'phone', '123-456-7890']
- ['email', 'phone']
- dict_items([('email', '[email protected]'), ('phone', '123-456-7890')])
- None
Answer: C) dict_items([('email', '[email protected]'), ('phone', '123-456-7890')])
Explanation: The items() method returns a view object of key-value pairs, represented as dict_items([('email', '[email protected]'), ('phone', '123-456-7890')]).
Question 13: Which of the following statements correctly describe the values() method? (Choose all that apply)
- It returns all the values in the dictionary.
- It returns a view object.
- It returns a list of tuples.
- It can be used in a loop to iterate over the values.
Answer:A) It returns all the values in the dictionary.
B) It returns a view object.
D) It can be used in a loop to iterate over the values.
Explanation: The values() method returns a view object containing all the values in the dictionary, which can be iterated over in a loop.
Question 14: Arrange the steps to correctly iterate through a dictionary and print both keys and values.
- Use the items() method.
- Iterate over the key-value pairs using a for loop.
- Print each key-value pair.
Answer: 1, 2, 3
Explanation: First, use the items() method to access the key-value pairs, then iterate over them using a loop, and finally print each pair.
Question 15: Complete the code to print all values from the dictionary data.
data = {'name': 'Alice', 'age': 30, 'city': 'New York'} for value in data.______(): print(value)▼
Answer: values
Explanation: The values() method returns a view object containing all the values in the dictionary, which are then printed in the loop.
Question 16: What will be the output of the following code?
car = {'make': 'Toyota', 'model': 'Corolla', 'year': 2020} for key, value in car.items(): print(f"{key}: {value}")
- ('make', 'Toyota') ('model', 'Corolla') ('year', 2020)
- make: Toyota model: Corolla year: 2020
- make model year
- None
Answer: B) make: Toyota model: Corolla year: 2020
Explanation: The items() method returns key-value pairs, which are printed using formatted strings.
17. Insert the correct method to retrieve only the keys from the dictionary scores.
scores = {'math': 95, 'science': 90, 'history': 85} keys = scores.______() print(list(keys))▼
Answer: keys
Explanation: The keys() method returns a view object of all the keys in the dictionary.
Question 18: What will be the result of the following code?
menu = {'burger': 5, 'fries': 2, 'soda': 1} for item in menu.items(): print(item)
- burger 5 fries 2 soda 1
- ('burger', 5) ('fries', 2) ('soda', 1)
- menu.keys()
- menu.items()
Answer: B) ('burger', 5) ('fries', 2) ('soda', 1)
Explanation: The items() method returns key-value pairs as tuples, and the loop prints each tuple.
Question 19: Which of the following are correct ways to iterate through a dictionary and access its keys or values? (Choose all that apply)
- for key in my_dict.keys():
- for value in my_dict.values():
- for key, value in my_dict.items():
- for item in my_dict.keys_values():
Answer: A) for key in my_dict.keys():
B) for value in my_dict.values():
C) for key, value in my_dict.items():
Explanation: Options A, B, and C correctly iterate through the dictionary's keys, values, and key-value pairs. Option D is incorrect as there is no keys_values() method.
Question 20: Arrange the steps to retrieve and print all the values from the dictionary contacts.
- Use the values() method.
- Convert the view object to a list.
- Print the list of values.
Answer: 1, 2, 3
Explanation: First, use the values() method to retrieve the values, then convert the view object to a list, and finally print the list.
Question 21: Complete the code to retrieve and print both keys and values from the dictionary library.
library = {'book': 'Python 101', 'author': 'John Doe'} for ______, ______ in library.items(): print(______, ______)▼
Answer: key, value, key, value
Explanation: The items() method returns key-value pairs, which can be unpacked into key and value in the loop.
Question 22: What will be the output of the following code?
devices = {'laptop': 1000, 'phone': 800, 'tablet': 600} print(list(devices.values()))
- [1000, 800, 600]
- laptop phone tablet
- {'laptop': 1000, 'phone': 800, 'tablet': 600}
- [('laptop', 1000), ('phone', 800), ('tablet', 600)]
Answer: A) [1000, 800, 600]
Explanation: The values() method returns a view object of all the values in the dictionary, and converting it to a list results in [1000, 800, 600].
Question 23: Insert the correct method to retrieve all the key-value pairs from the dictionary preferences.
preferences = {'theme': 'dark', 'notifications': True} items = preferences.______() print(list(items))▼
Answer: items
Explanation: The items() method returns a view object of key-value pairs.
Question 24: What will be the result of the following code?
grades = {'math': 'A', 'science': 'B', 'history': 'A'} keys_list = list(grades.keys()) print(keys_list)
- ['math', 'science', 'history']
- ['A', 'B', 'A']
- ('math', 'science', 'history')
- None
Answer: A) ['math', 'science', 'history']
Explanation: The keys() method returns the keys of the dictionary as a view object, which is then converted to a list and printed.
Question 25: Which of the following methods return a view object from a dictionary? (Choose all that apply)
- keys()
- values()
- items()
- get()
Answer: A) keys()
B) values()
C) items()
Explanation: The keys(), values(), and items() methods return view objects of the keys, values, and key-value pairs in the dictionary. The get() method returns the value associated with a specified key, not a view object.
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-methods-keys-items-and-values.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics