double d = 4.7;
int rounded = (int)(d + 0.5);
System.out.println(rounded);What is printed?
- A
6
- B
4
- Ccheck_circle
5
- D
5.0
Explanation
Adding 0.5 then truncating performs rounding for positive numbers; 4.7 + 0.5 = 5.2, cast to int gives 5.
AP Computer Science A· difficulty 4/5
double d = 4.7;
int rounded = (int)(d + 0.5);
System.out.println(rounded);What is printed?
6
4
5
5.0
Explanation
Adding 0.5 then truncating performs rounding for positive numbers; 4.7 + 0.5 = 5.2, cast to int gives 5.
Want 10 more like this — adaptive to your weak spots?