w3resource

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?

  1. items()
  2. keys()
  3. values()
  4. 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)

  1. for value in my_dict:
  2. for value in my_dict.values():
  3. for key, value in my_dict.items():
  4. 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)
  1. name Alice age 30
  2. Alice 30
  3. name age
  4. 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])
  1. name age
  2. Bob 25
  3. ('name', 'age')
  4. 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)

  1. for key, value in my_dict:
  2. for key, value in my_dict.items():
  3. for key in my_dict:
  4. 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.

  1. Print each value.
  2. Use the values() method to get a view object of the values.
  3. 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])
  1. apple 1 banana 2 cherry 3
  2. 1 2 3
  3. apple banana cherry
  4. 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)
  1. Prints b
  2. Prints 2
  3. Prints None
  4. 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)

  1. for k, v in data.items(): print(k, v)
  2. for k in data.keys(): print(k, data[k])
  3. for v in data.values(): print(v)
  4. 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.

  1. Use an if statement to check if the value is greater than 50.
  2. Iterate over the key-value pairs using items().
  3. 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=", ")
  1. x, y, z,
  2. 100, 200, 300,
  3. x y z
  4. 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])
  1. Prints math science history
  2. Prints 95 90 85
  3. Prints subject
  4. 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)

  1. for key in my_dict:
  2. for key in my_dict.keys():
  3. for key, value in my_dict.items():
  4. 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.

  1. Initialize a variable to hold the maximum value.
  2. Update the maximum value if the current value is greater.
  3. 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)
  1. x y
  2. y z
  3. 10 20 30
  4. 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()?

  1. Both keys and values are iterated over.
  2. Only the keys are iterated over, and a ValueError is raised.
  3. A TypeError is raised.
  4. 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)

  1. items()
  2. keys()
  3. values()
  4. 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.



Become a Patron!

Follow us on Facebook and Twitter for latest update.