PCEP-30-02 · Question #113
What is the expected output of the following code? ``python x = True y = False z = False if x or y and z: print('TRUE') else: print('FALSE') ``
The correct answer is B. TRUE. Option B is correct because Python's operator precedence rules evaluate and before or, so the expression x or y and z is parsed as x or (y and z). Since y and z evaluates to False and False = False, the full expression becomes True or False, which is True - triggering the if…
Question
x = True
y = False
z = False
if x or y and z:
print('TRUE')
else:
print('FALSE')
Options
- AThe code is erroneous.
- BTRUE
- CFALSE
- DNone of the above.
How the community answered
(52 responses)- A13% (7)
- B75% (39)
- C8% (4)
- D4% (2)
Explanation
Option B is correct because Python's operator precedence rules evaluate and before or, so the expression x or y and z is parsed as x or (y and z). Since y and z evaluates to False and False = False, the full expression becomes True or False, which is True - triggering the if branch and printing TRUE.
Option A is wrong because the code is perfectly valid Python - no syntax or runtime errors occur. Option C is wrong because a beginner might mistakenly read the expression left-to-right as (x or y) and z, which would yield True and False = False, but that's not how Python evaluates it. Option D is wrong because B is a valid correct answer.
Memory tip: Think of and as multiplication and or as addition - in math, multiplication always happens before addition (2 + 3 × 0 = 2, not 0). The same logic applies here: and binds tighter, so evaluate it first before or.
Community Discussion
No community discussion yet for this question.