PCEP Certification Practice: Iterating through Dictionaries and their Keys 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 "iterating through dictionaries and their keys 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: Which method is used to iterate over the keys of a dictionary?
- items()
- keys()
- values()
- get()
Answer: B) keys()
Explanation: The keys() method returns a view object containing the keys of the dictionary, which can be used for iteration.
Question 2: Which of the following methods can be used to iterate through the values of a dictionary? (Choose all that apply)
- for value in my_dict:
- for value in my_dict.values():
- for key, value in my_dict.items():
- for value in my_dict.keys():
Answer: B) for value in my_dict.values():
C) for key, value in my_dict.items():
Explanation: The values() method returns the values in the dictionary, and items() allows you to iterate over both keys and values. Iterating directly over my_dict iterates over the keys, not the values.
Question 3: Complete the code to iterate over the keys of the dictionary my_dict.
my_dict = {'a': 1, 'b': 2, 'c': 3} for ______ in my_dict: print(______ )▼
Answer: key, key
Explanation: Iterating directly over my_dict iterates over the keys. The loop variable key will take each key from the dictionary.
Question 4: What will be the output of the following code?
my_dict = {'name': 'Alice', 'age': 30} for key in my_dict: print(key)
- name Alice age 30
- Alice 30
- name age
- None
Answer: C) name age
Explanation:Iterating over the dictionary my_dict directly iterates over the keys, so the output will be name and age..
Question 5: Insert the correct method to iterate over both keys and values of the dictionary person.
person = {'name': 'Alice', 'age': 30} for key, value in person.______(): print(key, value)▼
Answer: items
Explanation: The items() method returns a view object that contains tuples of key-value pairs, which can be used for iteration.
Question 6: What will be the result of the following code?
person = {'name': 'Bob', 'age': 25} for key in person.keys(): print(person[key])
- name age
- Bob 25
- ('name', 'age')
- None
Answer: B) Bob 25
Explanation: The code iterates over the keys of the dictionary and uses each key to access and print the corresponding value.
Question 7: Which of the following loops correctly iterate over both keys and values of a dictionary? (Choose all that apply)
- for key, value in my_dict:
- for key, value in my_dict.items():
- for key in my_dict:
- for key in my_dict.keys():
Answer: B) for key, value in my_dict.items():
C) for key in my_dict:
Explanation: Option B iterates over both keys and values, while Option C iterates over the keys. Option D iterates over the keys using keys(), but Option A is incorrect because my_dict alone does not yield both keys and values.
Question 8: Arrange the steps to correctly iterate over the values of a dictionary and print each one.
- Print each value.
- Use the values() method to get a view object of the values.
- Iterate over the dictionary values using a for loop.
Answer: 2, 3, 1
Explanation: First, use the values() method to get the dictionary values, then iterate over them using a for loop, and finally print each value.
Question 9: Complete the code to print both the keys and values of the dictionary my_dict.
my_dict = {'x': 10, 'y': 20, 'z': 30} for ______, ______ in my_dict.items(): print(______, ______)▼
Answer: key, value, key, value
Explanation: The items() method provides both keys and values, and the loop variables key and value are used to print them.
Question 10: What will be the output of the following code?
my_dict = {'apple': 1, 'banana': 2, 'cherry': 3} for fruit in my_dict: print(fruit, my_dict[fruit])
- apple 1 banana 2 cherry 3
- 1 2 3
- apple banana cherry
- None
Answer: A) apple 1 banana 2 cherry 3
Explanation: The code iterates over the keys of the dictionary and prints each key along with its corresponding value.
Question 11: Insert the correct method to iterate over the values of the dictionary scores.
scores = {'math': 90, 'science': 85, 'english': 88} for score in scores.______(): print(score)▼
Answer: values
Explanation: The values() method allows iteration over the values in the dictionary, so the loop prints each score.
Question 12: What does the following code do?
my_dict = {'a': 1, 'b': 2, 'c': 3} for key, value in my_dict.items(): if value == 2: print(key)
- Prints b
- Prints 2
- Prints None
- Prints a b c
Answer: A) Prints b
Explanation: The code iterates over the key-value pairs and prints the key 'b' when the value is 2.
Question 13: Which of the following statements correctly iterate over the dictionary data and print both keys and values? (Choose all that apply)
- for k, v in data.items(): print(k, v)
- for k in data.keys(): print(k, data[k])
- for v in data.values(): print(v)
- for item in data: print(item)
Answer: A) for k, v in data.items(): print(k, v)
B) for k in data.keys(): print(k, data[k])
Explanation: Option A directly iterates over key-value pairs using items(), while Option B iterates over the keys and accesses each value using the key. Option C only prints values, and Option D only prints keys.
Question 14: Arrange the steps to find and print all keys in a dictionary that have a value greater than 50.
- Use an if statement to check if the value is greater than 50.
- Iterate over the key-value pairs using items().
- Print the key if the condition is met.
Answer: 2, 1, 3
Explanation: First, iterate over the dictionary with items(), then check if the value is greater than 50, and print the key if the condition is met.
Question 15: Complete the code to iterate over a dictionary data and print all keys that have a value of True.
data = {'a': True, 'b': False, 'c': True} for key, value in data.items(): if ______: print(key)▼
Answer: value
Explanation: The code checks if the value is True and prints the corresponding key.
Question 16: What will be the output of the following code?
my_dict = {'x': 100, 'y': 200, 'z': 300} for k in my_dict: print(k, end=", ")
- x, y, z,
- 100, 200, 300,
- x y z
- None
Answer: A) x, y, z,
Explanation: The code iterates over the keys of the dictionary and prints them separated by commas.
17. Insert the correct method to iterate over both keys and values of the dictionary info.
info = {'name': 'Alice', 'age': 30} for key, value in info.______(): print(key, ":", value)▼
Answer: items
Explanation: The items() method allows iteration over both keys and values in the dictionary.
Question 18: What does the following code do?
scores = {'math': 95, 'science': 90, 'history': 85} for subject in scores: print(scores[subject])
- Prints math science history
- Prints 95 90 85
- Prints subject
- Prints None
Answer: B) Prints 95 90 85
Explanation: The code iterates over the keys of the dictionary and prints the corresponding values.
Question 19: Which of the following are correct ways to iterate over the keys of a dictionary? (Choose all that apply)
- for key in my_dict:
- for key in my_dict.keys():
- for key, value in my_dict.items():
- for value in my_dict.values():
Answer: A) for key in my_dict:
B) for key in my_dict.keys():
Explanation: Both A and B correctly iterate over the keys of the dictionary. Option C iterates over both keys and values, and Option D iterates over values.
Question 20: Arrange the steps to correctly iterate over the values in a dictionary and find the maximum value.
- Initialize a variable to hold the maximum value.
- Update the maximum value if the current value is greater.
- Iterate over the values using the values() method.
Answer: 1, 3, 2
Explanation: First, initialize the maximum value, then iterate over the values, and update the maximum value if a larger one is found.
Question 21: Complete the code to print the sum of all values in the dictionary numbers.
numbers = {'a': 1, 'b': 2, 'c': 3} total = 0 for value in numbers.______(): total += value print(total)▼
Answer: values
Explanation: The values() method is used to access the values, which are then summed up to get the total.
Question 22: What will be the output of the following code?
data = {'x': 10, 'y': 20, 'z': 30} for k, v in data.items(): if v > 15: print(k)
- x y
- y z
- 10 20 30
- None
Answer: B) y z
Explanation: The code prints the keys of the dictionary where the value is greater than 15, which are y and z.
Question 23: Insert the correct code to print both the keys and values of the dictionary my_dict.
my_dict = {'a': 1, 'b': 2, 'c': 3} for ______, ______ in my_dict.items(): print(______, ______)▼
Answer: key, value, key, value
Explanation: The loop variables key and value are used to iterate over and print the key-value pairs.
Question 24: What happens if you try to iterate over a dictionary using for key, value in my_dict without using items()?
- Both keys and values are iterated over.
- Only the keys are iterated over, and a ValueError is raised.
- A TypeError is raised.
- The code runs without error but only iterates over the keys.
Answer: D) The code runs without error but only iterates over the keys.
Explanation: The code runs without error but only iterates over the keys.
Question 25: Which of the following methods can be used to access both keys and values in a dictionary during iteration? (Choose all that apply)
- items()
- keys()
- values()
- get()
Answer: A) items()
Explanation: The items() method returns both keys and values as pairs, while keys() and values() only return keys and values, respectively. The get() method is used to retrieve a single value by its key.
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://www.w3resource.com/python/certificate/data-collections-dictionaries-iterating-through-dictionaries-and-their-keys-and-values.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics