w3resource

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?

  1. A list of all the values in the dictionary.
  2. A view object of all the keys in the dictionary.
  3. A list of key-value pairs in the dictionary.
  4. 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)

  1. It returns a list of all the keys in the dictionary.
  2. It returns a view object of key-value pairs.
  3. Each element in the view object returned is a tuple.
  4. 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()))
  1. ['x', 'y', 'z']
  2. [(10, 20, 30)]
  3. [10, 20, 30]
  4. [('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)
  1. Python beginner
  2. course level
  3. ('course', 'Python') ('level', 'beginner')
  4. 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)

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

  1. Convert the view object to a list.
  2. Use the keys() method to get the keys.
  3. 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)
  1. 3 5 7
  2. apple banana cherry
  3. apple: 3 banana: 5 cherry: 7
  4. 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())
  1. ['email', '[email protected]', 'phone', '123-456-7890']
  2. ['email', 'phone']
  3. dict_items([('email', '[email protected]'), ('phone', '123-456-7890')])
  4. 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)

  1. It returns all the values in the dictionary.
  2. It returns a view object.
  3. It returns a list of tuples.
  4. 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.

  1. Use the items() method.
  2. Iterate over the key-value pairs using a for loop.
  3. 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}")
  1. ('make', 'Toyota') ('model', 'Corolla') ('year', 2020)
  2. make: Toyota model: Corolla year: 2020
  3. make model year
  4. 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)
  1. burger 5 fries 2 soda 1
  2. ('burger', 5) ('fries', 2) ('soda', 1)
  3. menu.keys()
  4. 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.

  1. Use the values() method.
  2. Convert the view object to a list.
  3. 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()))
  1. [1000, 800, 600]
  2. laptop phone tablet
  3. {'laptop': 1000, 'phone': 800, 'tablet': 600}
  4. [('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)
  1. ['math', 'science', 'history']
  2. ['A', 'B', 'A']
  3. ('math', 'science', 'history')
  4. 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)

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



Become a Patron!

Follow us on Facebook and Twitter for latest update.

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