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 questions

Domain Data Collections – Tuples, Dictionaries, Lists, and Strings

Answer options
A: The code is erroneous. Correct answer
B: 0 1
C: 0 0
D: 1 0
Correct Answer:
A. The code is erroneous.
Explanation

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.

Why Incorrect

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.

References

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.

Scroll to Top