Following code will result in:

int a1 = 5; 
double a2 = (float)a1;
  1. Compilation error

  2. Runtime error

  3. No errors

  4. Execptions


Correct Option: C

AI Explanation

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.

Find more quizzes: