PCAP-31-03 Exam QuestionsBrowse all questions from this exam

PCAP-31-03 Exam - Question 34


What is the expected behavior of the following code?

x =3 % 1

y = 1 if x > 0 else 0

print(y)

Show Answer
Correct Answer: C

The code assigns the result of the expression 3 % 1 to the variable x. The modulo operation 3 % 1 returns 0 because 3 divided by 1 leaves a remainder of 0. The next line uses a conditional expression to assign 1 to y if x > 0, otherwise it assigns 0. Since x is 0, the condition x > 0 evaluates to false, so y is assigned 0. Therefore, the code prints 0.

Discussion

3 comments
Sign in to comment
a4129fdOption: C
May 30, 2024

As tested in my IDE

DKAT2023Option: C
Jul 1, 2024

C is correct

Dave304409Option: C
Jul 5, 2024

is correct