126k views
5 votes
What value will z have if we execute the following assignment statement?

int z = 5 / 10;
A) z will equal 0.0
B) z will equal 0.5
C) z will equal 5.0
D) none of the above; a run-time error arises because z is double and 5/10 is an int

1 Answer

4 votes

Final answer:

The value of z in the given assignment statement will be 0.0

Step-by-step explanation:

To evaluate the assignment statement, we need to understand how the division operator works with integers. When two integers are divided, the result is an integer, and any decimal part is truncated. In the given assignment statement, 5 divided by 10 is 0.5, but since both numbers are integers, the result will be truncated to an integer. Therefore, the value of z will be 0.

So the answer is A) z will equal 0.0.

User Richard Purdie
by
8.3k points