PCEP-30-02 · Question #61
What value will be assigned to the x variable? z = 3 y = 7 x = y < z and z > y or y > z and z < y
The correct answer is B. True. Option B is correct because Python evaluates boolean expressions using operator precedence: comparison operators first, then and, then or. The expression groups as (y < z and z > y) or (y > z and z < y) - the left group (7 < 3 and 3 > 7) evaluates to False, but the right group…
Question
Options
- A0
- BTrue
- CFalse
- D1
How the community answered
(39 responses)- A15% (6)
- B69% (27)
- C10% (4)
- D5% (2)
Explanation
Option B is correct because Python evaluates boolean expressions using operator precedence: comparison operators first, then and, then or. The expression groups as (y < z and z > y) or (y > z and z < y) - the left group (7 < 3 and 3 > 7) evaluates to False, but the right group (7 > 3 and 3 < 7) evaluates to True, so False or True yields True, which is assigned to x.
A (0) is wrong because Python assigns the actual boolean object True, not the integer equivalent - True only equals 0 if you're thinking of False (which equals 0), not True (which equals 1). C (False) is wrong because it's tempting to stop at the first and group (which is False) and ignore the or clause that follows. D (1) is wrong for the same reason as A - True == 1 numerically, but the variable holds the boolean True, not the integer 1.
Memory tip: Think of and/or like multiplication/addition - and binds tighter (like *), so group and clauses first, then chain them with or. If any and-group is True, the whole or expression is True.
Community Discussion
No community discussion yet for this question.