nerdexam
Python_Institute

PCEP-30-02 · Question #67

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

The correct answer is B. 17.5. Option B (17.5) is correct because Python evaluates this expression using operator precedence: exponentiation (`) first, then division operators (/, //), then addition. This gives 42 = 16, 1/2 = 0.5 (true division always returns a float), 3//3 = 1, and finally 0.5 + 1 + 16 =…

Question

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

Options

  • A17
  • B17.5
  • C8.5
  • D8

How the community answered

(33 responses)
  • A
    9% (3)
  • B
    82% (27)
  • C
    3% (1)
  • D
    6% (2)

Explanation

Option B (17.5) is correct because Python evaluates this expression using operator precedence: exponentiation (**) first, then division operators (/, //), then addition. This gives 4**2 = 16, 1/2 = 0.5 (true division always returns a float), 3//3 = 1, and finally 0.5 + 1 + 16 = 17.5.

A (17) is wrong because it treats 1/2 as integer (floor) division yielding 0 - that was Python 2 behavior; in Python 3, / always returns a float. C (8.5) likely results from misreading 4**2 as 4/2 or another reduced value. D (8) likely comes from applying integer division throughout and misreading the exponentiation operator.

Memory tip: Think of Python's order as "E before MD before AS" - Exponentiation, then Multiplication/Division (including // and %), then Addition/Subtraction - and remember that a single / in Python 3 always produces a float, even for whole numbers like 1/1 = 1.0.

Community Discussion

No community discussion yet for this question.

Full PCEP-30-02 Practice