PCEP-30-02 · Question #41
What is the expected output of the following code? data = {'1': '0', '0': '1'} for d in data.vals(): print(d, end='')
The correct answer is C. The code is erroneous. Option C is correct because data.vals() is not a valid Python method - dictionaries use .values() (with a full "values"), so this code raises an AttributeError at runtime before any output is produced. Options A, B, and D all assume the code runs successfully, which it cannot…
Question
Options
- A01
- B10
- CThe code is erroneous.
- D00
How the community answered
(47 responses)- A4% (2)
- B9% (4)
- C72% (34)
- D15% (7)
Explanation
Option C is correct because data.vals() is not a valid Python method - dictionaries use .values() (with a full "values"), so this code raises an AttributeError at runtime before any output is produced.
Options A, B, and D all assume the code runs successfully, which it cannot. If the typo were fixed to .values(), Python 3.7+ preserves insertion order, so the values '0' then '1' would print as 01 (making A the would-be correct answer) - not 10 (B) or 00 (D), which don't match the dict's values in any order.
Memory tip: Remember the full word - .keys(), .values(), .items() - Python dict methods are never abbreviated. If you see .vals(), .keys2(), or similar shorthand on an exam, it's always a trap for "erroneous code."
Community Discussion
No community discussion yet for this question.