Following code will result in:
int a1 = 5;
double a2 = (float)a1;
-
Compilation error
-
Runtime error
-
No errors
-
Execptions
The code casts an int to float, then assigns to double. This is valid - int can be implicitly converted to float, and float can be implicitly converted to double ( widening conversions). No compilation or runtime error occurs.
To answer this question, we need to understand the concept of type casting in Java.
In the given code, we have an integer variable a1 initialized with the value 5. Then, we have a double variable a2 which is assigned the value of a1 after type casting it to a float.
Now, let's go through each option to determine the correct answer:
Option A) Compilation error - This option is incorrect. The code will compile successfully because the type casting from int to float is allowed in Java.
Option B) Runtime error - This option is incorrect. The code will not result in a runtime error as the type casting is valid.
Option C) No errors - This option is correct. The code will execute without any compilation or runtime errors.
Option D) Exceptions - This option is incorrect. The code will not throw any exceptions.
Therefore, the correct answer is C) No errors. This option is correct because the code will execute without any compilation or runtime errors.