
Patrick B. answered 10/12/20
Math and computer tutor/teacher
option E is definitely correct
Ashy A.
asked 10/12/20The following code segment is intended to round val to the nearest integer and print the result.
double val = -0.7;
int roundedVal = (int) (val + 0.5);
System.out.println(roundedVal);
Which of the following best describes the behavior of the code segment?
a) The code segment works as intended
b) The code segment does not work as intended because val and roundedVal should be declared as the same data type.
c) The code segment does not work as intended because the expression (val + 0.5) should be cast to a double instead of an int.
d)The code segment does not work as intended because val should be cast to an int before 0.5 is added to it.
e) The code segment does not work as intended because the expression (int) (val + 0.5) rounds to the nearest integer only when val is positive.
Patrick B. answered 10/12/20
Math and computer tutor/teacher
option E is definitely correct
Get a free answer to a quick problem.
Most questions answered within 4 hours.
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.