Certified Associate in Python Programming (PCAP-31-03) — Question 9
What is the expected behavior of the following code?
x =3 % 1
y = 1 if x > 0 else 0
print(y)
Answer options
- A. the code is erroneus and it will not execute
- B. it outputs 1
- C. it outputs 0
- D. it outputs -1
Correct answer: C
Explanation
The expression '3 % 1' evaluates to 0, since 3 divided by 1 leaves no remainder. Consequently, 'y' is assigned the value 0 because the condition 'x > 0' evaluates to false. Therefore, the correct output is 0, making option C correct, while options A, B, and D are incorrect.