nerdexam
Python_Institute

PCEP-30-02 · Question #79

What is the expected output of the following code? x = 1 + 1 // 2 + 1 / 2 + 2 print(x)

The correct answer is D. 3.5. Option D is correct because Python evaluates // (floor division) and / (true division) before +, so the expression becomes 1 + (1 // 2) + (1 / 2) + 2: 1 // 2 yields 0 (floor division truncates toward negative infinity), and 1 / 2 yields 0.5 (true division always returns a…

Question

What is the expected output of the following code? x = 1 + 1 // 2 + 1 / 2 + 2 print(x)

Options

  • A3
  • B4.0
  • C4
  • D3.5

How the community answered

(61 responses)
  • A
    2% (1)
  • B
    13% (8)
  • C
    5% (3)
  • D
    80% (49)

Explanation

Option D is correct because Python evaluates // (floor division) and / (true division) before +, so the expression becomes 1 + (1 // 2) + (1 / 2) + 2: 1 // 2 yields 0 (floor division truncates toward negative infinity), and 1 / 2 yields 0.5 (true division always returns a float), giving 1 + 0 + 0.5 + 2 = 3.5.

A (3) is wrong because it ignores that 1 / 2 produces 0.5, not 0 - a common mistake from languages where / between integers truncates. C (4) makes the same error, treating both / and // as integer division. B (4.0) doesn't correspond to any plausible reading of the expression and likely results from miscalculating the floor division term as 1 instead of 0.

Memory tip: Think "double-slash, double-down" - // always rounds down (floor), while a single / always gives a float in Python 3. When you see both operators in one expression, handle them left-to-right before adding anything up.

Community Discussion

No community discussion yet for this question.

Full PCEP-30-02 Practice