What value will z have if we execute double z = 5 / 10;


What value will z have if we execute the following assignment statement?


double z = 5 / 10;

a) z will equal 0.0

b) z will equal 0.5

c) z will equal 5.0

d) z will equal 0.05

e) none of the above, a run-time error arises because z is a double and 5 / 10 is an int

Answer: a. Explanation: 5 and 10 are both int values, so 5 / 10 is an integer division. The result is 0. Even though z is a double and can store the real answer, 0.5, it only gets 0 because of the integer division. In order to get 0.5, we would have to first cast 5 or 10 as a double.