PCEP-30-02 · Question #115
What is the expected output of the following code? ``python x = 4.5 y = 2 print (x // y) ``
The correct answer is D. 2.0. Option D (2.0) is correct because // is Python's floor division operator, which divides and then rounds down to the nearest whole number - 4.5 / 2 = 2.25, floored to 2 - but since x is a float, Python preserves the float type and returns 2.0 instead of the integer 2. A (2.5) is…
Question
x = 4.5
y = 2
print (x // y)
Options
- A2.5
- B2
- C2.25
- D2.0
How the community answered
(52 responses)- A10% (5)
- B4% (2)
- C2% (1)
- D85% (44)
Explanation
Option D (2.0) is correct because // is Python's floor division operator, which divides and then rounds down to the nearest whole number - 4.5 / 2 = 2.25, floored to 2 - but since x is a float, Python preserves the float type and returns 2.0 instead of the integer 2.
- A (2.5) is wrong because no standard division of 4.5 by 2 produces 2.5; that's simply an arithmetic error.
- C (2.25) is wrong because
2.25is the result of regular division (/), not floor division (//). - B (2) is the most tempting distractor - the floor operation does produce the value
2, but because at least one operand is a float, Python always returns a float result, making it2.0not2(anint).
Memory tip: Think of // as "divide then floor," but remember the type rule: float // anything stays a float. If you see a float operand, expect a .0 in the result.
Community Discussion
No community discussion yet for this question.