Following code will result in: class A { int b = 1; public static void main(String [] args) { System.out.println("b is " + b); }}

  1. Compilation error

  2. Runtime Error

  3. Runtime Exception

  4. Output of b is 1


Correct Option: A

AI Explanation

To answer this question, let's go through each option to understand why it is correct or incorrect:

Option A) Compilation error - This option is correct. The code will result in a compilation error. The reason is that the variable b is an instance variable of class A, but the main method is a static method. Static methods cannot directly access instance variables. In order to access the instance variable b within the static method main, you would need to create an instance of class A and access the variable through that instance.

Option B) Runtime Error - This option is incorrect. There will not be a runtime error in this code.

Option C) Runtime Exception - This option is incorrect. There will not be a runtime exception in this code.

Option D) Output of b is 1 - This option is incorrect. The code will not compile, so there will be no output.

The correct answer is Option A) Compilation error. This option is correct because the code will result in a compilation error due to the attempt to access the instance variable b within a static method.

Find more quizzes: