PCEP-30-02 · Question #108
What value will be assigned to the x variable? `` 1 z = 2 2 y = 1 3 x = y < z or z > y and y > z or z < y ``
The correct answer is D. 1. Option D is correct because Python's and operator binds more tightly than or, so the expression is parsed as (y < z) or (z > y and y > z) or (z < y) - evaluating to True or (True and False) or False → True or False or False → True. Since Python's bool is a subclass of int and…
Question
1 z = 2
2 y = 1
3 x = y < z or z > y and y > z or z < y
Options
- AFalse
- B0
- CTrue
- D1
How the community answered
(67 responses)- A3% (2)
- B15% (10)
- C7% (5)
- D75% (50)
Explanation
Option D is correct because Python's and operator binds more tightly than or, so the expression is parsed as (y < z) or (z > y and y > z) or (z < y) - evaluating to True or (True and False) or False → True or False or False → True. Since Python's bool is a subclass of int and True is literally equal to 1, the value stored in x is 1. Option A (False) and B (0) are wrong because the first sub-expression y < z (i.e., 1 < 2) is truthy, making the entire or chain truthy via short-circuit evaluation. Option C (True) is the close distractor - while x does hold True, the question targets the underlying integer value, since True == 1 and int(True) returns 1. Memory tip: Think of Python booleans as dressed-up integers - True is always 1 and False is always 0, and and always "binds before" or just like multiplication binds before addition in arithmetic.
Community Discussion
No community discussion yet for this question.