PCEP-30-02 · Question #276
What is the expected output of the following code if the user enters 3 and 2? ``python x = int(input()) y = int(input()) x = x % y x = x % y y = y % x print(y) ``
The correct answer is D. 0. Tracing through the code step by step reveals why D (0) is correct: after x = 3 % 2 = 1, then x = 1 % 2 = 1 (unchanged, since 1 < 2), and finally y = 2 % 1 = 0 - any integer modulo 1 is always 0, so print(y) outputs 0. A (1) is wrong because 1 is the value of x after both…
Question
x = int(input())
y = int(input())
x = x % y
x = x % y
y = y % x
print(y)
Options
- A1
- B2
- C3
- D0
How the community answered
(17 responses)- A12% (2)
- B6% (1)
- C6% (1)
- D76% (13)
Explanation
Tracing through the code step by step reveals why D (0) is correct: after x = 3 % 2 = 1, then x = 1 % 2 = 1 (unchanged, since 1 < 2), and finally y = 2 % 1 = 0 - any integer modulo 1 is always 0, so print(y) outputs 0.
A (1) is wrong because 1 is the value of x after both modulo operations - a trap for students who stop reading one line too early. B (2) is wrong because it ignores the final reassignment of y entirely, treating the original input as the answer. C (3) is wrong because it's simply x's original input value, never printed or used after the first line.
Memory tip: When a variable is reassigned multiple times, physically write down its value after each line - the most common mistake on these questions is tracking the wrong variable or stopping one step too soon.
Community Discussion
No community discussion yet for this question.