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
Options
- A3
- B4
- C1
- D2
How the community answered
(20 responses)- A80% (16)
- B5% (1)
- C10% (2)
- D5% (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
ifconditionnot x or y=False or False=False- the block is skipped. - D (2) is wrong because the second
elifconditionnot x or (not y and z)=False or (True and False)=False- also skipped. - B (4) is wrong because the
elseis never reached; the thirdeliffires 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.