Professional Documents
Culture Documents
Data Types Data Types: 1. What Is The Result of Compiling and Running The Following Code?
Data Types Data Types: 1. What Is The Result of Compiling and Running The Following Code?
Data Types
Explanation:
floating-point numbers are by default of type double.
99.9 is a double not a float
to print “float” cast 99.9 to (float)
2. Which of the followings are correct when tried to compile and execute the below code ?
(choose 2)
class s
{
public static void main(String agr[])
{
short s1=4; //LINE 1
short s2 = s1+=s1; //LINE 2
short s3= s1+s2; //LINE 3
byte b1=(byte)s1 +(byte)s2; //LINE 4
byte b2=(byte)((byte)s1 +(byte)(byte)s2); //LINE 5
}
}
Explanation:
Every primitives( byte,short,int,long) in an expression is first converted into int then the
expression is evaluated now if we are assigning the result of the expression to narrower
primitives(like byte or short) without explicitly typecasting it then it will cause compiler
error.
LINE 3: Type mismatch: cannot convert from int to short
LINE 4: Type mismatch: cannot convert from int to byte
Explanation:
floating-point numbers are by default of type double.
99.9 is a double not a float
to print “float” cast 99.9 to (float)
Explanation:
line 1: int to float, simple widening
line 2: 1.0 + 1 = 2.0
Explanation:
floating-point numbers are by default of type double.
99.9 is a double not a float
The compiler will complain because it cannot find a method named test() that can
accept a double.
6. if byte b=50;
b=b*2;