PCAP 31 03 PCAP Certified Associate Python Programmer · Free Practice Question Medium
Question 45
Q302 - Operators
What value will be assigned to the x variable?
- z = 2
- y = 1
- x = y < z or z > y and y > z or z < y
-
A
0 -
B
False -
C
True -
D
1
Reveal correct answer
Correct answer: C
Explanation
Topics: operators relational operators operator precedence
Try it yourself:
- z = 2
- y = 1
- x = y < z or z > y and y > z or z < y
- print(x) # True
- print(y < z or z > y and y > z or z < y) # True
- print(1 < 2 or 2 > 1 and 1 > 2 or 2 < 1) # True
- print(True or True and False or False) # True
- print(True or (True and False) or False) # True
- print(True or False or False) # True
- print((True or False) or False) # True
- print(True or False) # True
- print(True) # True
Explanation:
The operators here are from three different groups.
"Comparisons, Identity, Membership operators", "Logical AND", "Logical OR".
The two comparison operators,
greater than operator and less than operator have the highest precedence.
Then the logical and operator has a higher precedence
than the logical or operators
(There are similar questions Q119 and Q241.)
Q302 (Please refer to this number, if you want to write me about this question.)
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
