nerdexam
Python_Institute

PCEP-30-02 · Question #76

What is the expected output of the following code? x = 9 y = 12 result = x // 2 2 / 2 + y % 2 * 3 print(result)

The correct answer is C. 8.0. Option C (8.0) is correct because Python evaluates the expression using strict operator precedence: ` binds tightest, so 2 3 = 8 first; then all //, , /, and % operators are resolved left to right, giving 9 // 2 2 / 2 → 4 * 2 / 2 → 8 / 2 → 4.0; and 12 % 8 = 4; finally 4.0 + 4 =…

Question

What is the expected output of the following code? x = 9 y = 12 result = x // 2 * 2 / 2 + y % 2 ** 3 print(result)

Options

  • A8
  • B7.0
  • C8.0
  • D9.0

How the community answered

(60 responses)
  • A
    8% (5)
  • B
    3% (2)
  • C
    73% (44)
  • D
    15% (9)

Explanation

Option C (8.0) is correct because Python evaluates the expression using strict operator precedence: ** binds tightest, so 2 ** 3 = 8 first; then all //, *, /, and % operators are resolved left to right, giving 9 // 2 * 2 / 2 → 4 * 2 / 2 → 8 / 2 → 4.0; and 12 % 8 = 4; finally 4.0 + 4 = 8.0 - a float because / always yields a float in Python 3. A (8) fails because / (true division) always returns a float, never an integer. B (7.0) results from misreading ** precedence - for example, confusing 2 ** 3 with 3 ** 2 = 9, making 12 % 9 = 3 and yielding 4.0 + 3 = 7.0. D (9.0) likely comes from miscalculating the left-hand sub-expression or misapplying grouping to the // operator.

Memory tip: Remember "PEMDAS but with Python's twist" - ** beats %, and / always floats; when you see a / anywhere in a chain, the whole result floats.

Community Discussion

No community discussion yet for this question.

Full PCEP-30-02 Practice