PCAP 31 03 PCAP Certified Associate Python Programmer · Free Practice Question Medium
Question 56
Q633 - Operators
What is the expected output of the following code?
x = 28
y = 8
print(x / y)
print(x // y)
print(x % y)
-
A
- 3
- 3.5
- 4
-
B
- 3.5
- 3
- 4
-
C
- 3.0
- 3
- 2
-
D
- 3.5
- 3.5
- 2
Reveal correct answer
Correct answer: B
Explanation
Topic: arithmetic operators
Try it yourself:
- a = 28
- b = 8
- print(a / b) # 3.5
- print(a // b) # 3
- print(a % b) # 4
Explanation:
Everything normal here.
The division operator the floor division operator
and the modulus operator all do their normal job.
Q633 (Please refer to this number, if you want to write me about this question.)
A. This choice is incorrect because the order of the outputs is not correct. The floor division (//) operation comes before the division (/) operation in the code, so the output of x // y should be printed before the output of x / y.
B. The code first calculates the result of x divided by y, which is 28 / 8 = 3.5. The division operator (/) in Python always returns a float value if one or both of the operands are floats. Then, it calculates the floor division of x by y, which is the integer division without the remainder, resulting in 28 // 8 = 3. Finally, it calculates the remainder of x divided by y, which is the modulus operation, resulting in 28 % 8 = 4.
C. This choice is incorrect because the output of the division operation (x / y) is 3.5, not 3.0. The division operator (/) in Python always returns a float value if one or both of the operands are floats, so 28 / 8 = 3.5.
D. This choice is incorrect because the output of the modulus operation (x % y) is 4, not 2. The modulus operator (%) returns the remainder of the division operation, which in this case is 28 % 8 = 4.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
