nerdexam
Python_Institute

PCEP-30-02 · Question #287

What is the expected output of the following code? x = True y = False z = False if not x or y: print(1) elif not x or not y and z: print(2) elif not x or y or not y and x: print(3) else: print(4)

The correct answer is A. 3. Option A is correct because the third elif condition evaluates to True. With x=True, y=False, z=False, the expression not x or y or not y and x is parsed (due to Python's precedence rules: not > and > or) as False or False or (True and True), which yields True, so 3 is printed…

Question

What is the expected output of the following code? x = True y = False z = False if not x or y: print(1) elif not x or not y and z: print(2) elif not x or y or not y and x: print(3) else: print(4)

Options

  • A3
  • B4
  • C1
  • D2

How the community answered

(20 responses)
  • A
    80% (16)
  • B
    5% (1)
  • C
    10% (2)
  • D
    5% (1)

Explanation

Option A is correct because the third elif condition evaluates to True. With x=True, y=False, z=False, the expression not x or y or not y and x is parsed (due to Python's precedence rules: not > and > or) as False or False or (True and True), which yields True, so 3 is printed.

Why the distractors fail:

  • C (1) is wrong because the first if condition not x or y = False or False = False - the block is skipped.
  • D (2) is wrong because the second elif condition not x or (not y and z) = False or (True and False) = False - also skipped.
  • B (4) is wrong because the else is never reached; the third elif fires first.

Memory tip: When evaluating chained boolean expressions, always apply Python's precedence order - not binds tightest, then and, then or - by mentally inserting parentheses around every and group before combining with or. This prevents the common mistake of reading left-to-right.

Community Discussion

No community discussion yet for this question.

Full PCEP-30-02 Practice