Question 521
double r = 7.5 % 2.0;
int q = 17 % 5;
System.out.println(r + " " + q);
What is printed when the code segment executes?Main Solution
Answer
Answer: C. % returns the remainder after division. For doubles, 7.5 % 2.0: the largest multiple of 2.0 that does not exceed 7.5 is 3 · 2.0 = 6.0, and 7.5 − 6.0 = 1.5. For ints, 17 % 5: the largest multiple of 5 that does not exceed 17 is 3 · 5 = 15, and 17 − 15 = 2. Option D incorrectly treats % as division.
Answer: Correct: C
A
1.0 2
B
1.5 3
C
1.5 2
D
3.75 2
Answer images

