Ashy A.

asked • 10/12/20

I have a cs question

The 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.

1 Expert Answer

By:

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.