Q40
What is the output of the following program? class A public static void main(String args[]) byte b; int i = 258; double d = 325.59; b = (byte) i; System.out.print(b); i = (int) d; System.out.print(i); b = (byte) d; System.out.print(b);
A.
258 325 325
B.
258 326 326
C.
2 325 69
AnswerD.
Error
Answer: Option C
Solution
Answer: Option C
Solution:
In the type cast
b = (byte) i;
The value of i i.e 258 is cast into a byte variable. Whenever a casting done where source value is greater than the range of the destination then the result is the remainder of the division of source by the range of the destination. In this case the result is the remainder of the division of 258 by 256(the range of a byte), which is 2.
In the second type cast
i = (int) d;
the fractional component of d is lost and hence gives the output 325.
In the third type cast