128k views
5 votes
This compiles:
float num = (int) 20.5f + 123.55;
a) True
b) False

1 Answer

6 votes

Final answer:

The code 'float num = (int) 20.5f + 123.55;' compiles correctly because the integer cast truncates the float to an integer before addition, and the result is promoted back to a float.

Step-by-step explanation:

The statement in question is 'float num = (int) 20.5f + 123.55;' and whether it compiles correctly is being asked. The answer is A. True. Here, an explicit cast is performed on the floating-point number 20.5f to an integer, which would truncate the decimal and result in 20. The integer is then added to 123.55, a floating-point number, and the result would be a float due to the addition operation promoting the integer to a float to operate with another floating-point number.

User Adrianmanduc
by
9.0k points