PCEP-30-02 practice question 34 of 35
What is the expected output of the following code? data = {'1': '0', '0': '1'} for d in data.vals(): print(d, end=' ')
Full set: 548 questionsDomain Data Collections – Tuples, Dictionaries, Lists, and Strings
The code will raise an AttributeError. The dict object in Python does not have a method named vals(). The correct method to retrieve a view object displaying a list of the dictionary's values is values(). Because the code calls a non-existent method, the Python interpreter will stop execution and report an error, preventing any output from being printed.
B. 0 1: This would be the potential output if the correct method data.values() were used, as dictionaries preserve insertion order in modern Python versions.
C. 0 0: This output is incorrect as the dictionary values are '0' and '1', not two instances of '0'.
D. 1 0: This might be the output if data.values() were used in an older Python version where dictionary order was not guaranteed, but the primary issue is the erroneous method call.
1. Python Software Foundation. (2023). The Python Standard Library: 5.5. Dictionaries. Python 3.12.0 documentation. Retrieved from https://docs.python.org/3/library/stdtypes.html#mapping-types-dict.
Section 5.5, dict.values(): The official documentation lists values() as the correct method to "Return a new view of the dictionary’s values." It does not list a vals() method, confirming that calling it would result in an AttributeError.
2. Guttag, J. V. (2016). 6.0001 Introduction to Computer Science and Programming in Python, Fall 2016. MIT OpenCourseWare.
Lecture 5 Notes, Section "Dictionaries (dict)": The course materials explicitly list the methods for dictionaries, including .keys(), .values(), and .items(). The method vals() is not mentioned, as it is not a standard dictionary method in Python.
3. Van Rossum, G., & Drake, F. L. (2023). The Python Language Reference: 6.2.5. Dictionaries. Python 3.12.0 documentation. Retrieved from https://docs.python.org/3/reference/datamodel.html#dictionaries.
This reference details the data model for dictionaries. A review of the available operations and methods for the dict type confirms the absence of a vals() method.
Keep practicing
Take the free PCEP-30-02 practice test
Ten exam-style questions with answers and explanations, plus the exam facts and study guides.
More questions
Other PCEP-30-02 practice questions
- Question 1The following list is given:
- Question 2What is the output of the following code? num = 10 while num >= 0: num -= 2 print(num)
- Question 4We need to check if at least one element of the iterable object evaluates to the truth. W…
- Question 5This question is a tricky one. What is the result of the following code snippet? numbers …
- Question 8What is the purpose of the continue statement in Python?
- Question 11We need to check if all elements of the iterable object evaluate to the truth. What built…
