Ashy A.

asked • 10/12/20

heres a cs question

Consider the following code segment, which is intended to find the average of two positive integers, x and y.

int x;

int y;

int sum = x + y;

double average = (double) (sum / 2);

Which of the following best describes the error, if any, in the code segment?



a) There is no error, and the code works as intended.


b) In the expression (double) (sum / 2), the cast to double is applied too late, so the average will be less than the expected result for even values of sum


c) In the expression (double) (sum / 2), the cast to double is applied too late, so the average will be greater than the expected result for even values of sum


d) In the expression (double) (sum / 2), the cast to double is applied too late, so the average will be less than the expected result for odd values of sum.


e) In the expression (double) (sum / 2), the cast to double is applied too late, so the average will be greater than the expected result for odd values of sum.

1 Expert Answer

By:

Patrick B. answered • 10/12/20

Tutor
4.7 (31)

Math and computer tutor/teacher

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.