Multiple choice

What is the output of following Java program?

public class Test { public static void main(String[] args) { double i=45.6567; float j=i; System.out.println( j ); } }

  1. 45

  2. 45.6567

  3. 0.6567

  4. Run time Exception

  5. None of the above

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

As variable 'i' is of double and 'j' is of float , we cannot assign a data type with lower precedence with data type of higher precedence. Explicit type cast must be done in order to assign a data type with lower precedence with a data type of higher precedence. i.e float j=(float)i; which type casts the double datatype to float data type As the explicit type casting is not done in the given program, hence we get run time exception.