Exam PCAP-31-03 All QuestionsBrowse all questions from this exam
Question 34

What is the expected behavior of the following code?

x =3 % 1

y = 1 if x > 0 else 0

print(y)

    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
a4129fdOption: C

As tested in my IDE

DKAT2023Option: C

C is correct

Dave304409Option: C

is correct