What will be the output of the following program?
Correct Answer : D
main, there are 4 variables of type
sum is an integer variable. The result of
l + i + s + b will be
i, s, b are automatically casted to
long while adding them to
l. But the result of this addition is stored in
sum which is of type
int. This leads to type mismatch. So the program does not compile. In order to avoid this explicitly cast
l + i + s + b to
int as shown below.
int sum = ((int)l) + i + s + b;
int sum = (int) (l + i + s + b);
Topic: Type Casting In Java