PCAP 31 03 PCAP Certified Associate Python Programmer · Free Practice Question Easy
Question 48
Q415 - Data Types
What is the expected output of the following code if the user enters 3 and 2?
- x = int(input())
- y = int(input())
- x = x % y
- x = x % y
- y = y % x
- print(y)
-
A
2 -
B
3 -
C
1 -
D
0
Reveal correct answer
Correct answer: D
Explanation
Topics: input() int() modulus operator
Try it yourself:
- # x = int(input()) # Input: 3
- # y = int(input()) # Input: 2
- x, y = 3, 2 # Just for convenience
- x = x % y
- print(3 % 2) # 1
- x = x % y
- print(1 % 2) # 1
- print(x)
- print(y)
- y = y % x
- print(2 % 1) # 0
- print(y) # 0
Explanation:
input() returns a string but the int() function casts them to an integer
Then there is a lot of the modulus operator but you just have to concentrate.
(There is a similar question with different values Q234.)
Q415 (Please refer to this number, if you want to write me about this question.)
A. This choice is incorrect because the code does not result in y being assigned the value of 2 based on the calculations performed. The final value of y is determined by the remainder of the initial y divided by the updated x, which results in y being 0, not 2.
B. This choice is incorrect because the code does not result in y being assigned the value of 3 based on the calculations performed. The final value of y is determined by the remainder of the initial y divided by the updated x, which results in y being 0, not 3.
C. This choice is incorrect because the code does not result in y being assigned the value of 1 based on the calculations performed. The final value of y is determined by the remainder of the initial y divided by the updated x, which results in y being 0, not 1.
D. The code first takes two integer inputs from the user, assigns the remainder of x divided by y to x, then assigns the remainder of the updated x divided by y to x again. Finally, it assigns the remainder of y divided by the updated x to y. Since x and y are both 3 and 2 respectively, after the calculations, y becomes 0, which is the expected output.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
