PCEP-30-02 · Question #95
What is the output of the following snippet? ``python y = 2 + 3 * 5. print(y) ``
The correct answer is C. 17.0. Option C (17.0) is correct because Python's operator precedence evaluates multiplication before addition, so 3 5. is computed first (yielding 15.0), then 2 + 15.0 = 17.0. The trailing dot in 5. is valid Python syntax for a float literal (5.0), and since one operand is a float…
Question
y = 2 + 3 * 5.
print(y)
Options
- A25.0
- BThe snippet will cause an execution error.
- C17.0
- D17
How the community answered
(25 responses)- A4% (1)
- B4% (1)
- C80% (20)
- D12% (3)
Explanation
Option C (17.0) is correct because Python's operator precedence evaluates multiplication before addition, so 3 * 5. is computed first (yielding 15.0), then 2 + 15.0 = 17.0. The trailing dot in 5. is valid Python syntax for a float literal (5.0), and since one operand is a float, the entire result is promoted to float.
Why the distractors are wrong:
- A (25.0): This would result from left-to-right evaluation (
(2 + 3) * 5. = 25.0), ignoring that*has higher precedence than+. - B (execution error):
5.is perfectly legal Python - a trailing dot is a valid way to write a float literal; no error occurs. - D (17): The integer
17would only result if all operands were integers, but the float5.forces the result to be17.0, not17.
Memory tip: Remember "PEMDAS" for precedence, and "one float infects the result" - whenever any operand in an arithmetic expression is a float, Python returns a float. Watch for sneaky float literals like 5. on exams.
Community Discussion
No community discussion yet for this question.