Following code will result in:

class A { 
   public static void main(String [] args) {
       B b = new A(); 
   }
} 
class B extends A {}
  1. Compilation error

  2. Runtime Error

  3. Runtime Exception

  4. Other Execptions


Correct Option: A

AI Explanation

To answer this question, let's go through each option:

Option A) Compilation error - This option is correct. The code will result in a compilation error. The reason is that in the main method, an instance of class A is being assigned to a variable of type B. However, class A is not a subclass of class B, so this assignment is not allowed. This will result in a compilation error.

Option B) Runtime Error - This option is incorrect. A runtime error occurs when the code executes and encounters an error that prevents it from continuing. In this case, the code will not reach the runtime stage because it fails to compile.

Option C) Runtime Exception - This option is incorrect. A runtime exception occurs when an exception is thrown during the execution of the code. In this case, the code will not reach the runtime stage because it fails to compile.

Option D) Other Exceptions - This option is incorrect. Since the code fails to compile, it will not reach the stage where other exceptions can occur.

The correct answer is A) Compilation error. This option is correct because the code fails to compile due to the mismatch in the assignment of an instance of class A to a variable of type B.

Find more quizzes: