Sail E0 Webinar
Question


What is the output of this program?


class conversion {
public static void main(String args[])
{
double a = 295.04;
int b = 300;
byte c = (byte) a;
byte d = (byte) b;
System.out.println(c + " " + d);
}
}
Options:
A .  38 43
B .  39 44
C .  295 300
D .  295.04 300
Answer: Option B

Type casting a larger variable into a smaller variable results in modulo of larger variable by 

range of smaller variable. b contains 300 which is larger than byte's range i:e -128 to 127 

hence d contains 300 modulo 256 i:e 44.
output:
$ javac conversion.java
$ java conversion
39 44




Was this answer helpful ?
Next Question

Submit Solution

Your email address will not be published. Required fields are marked *

Latest Videos

Latest Test Papers