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, you need to understand the concept of static and instance variables in Java.

In the given code, the variable b is an instance variable of the class A because it is declared within the class but outside any method or constructor.

The main method in Java is a static method, which means it belongs to the class itself and not to any specific instance of the class. Static methods can only access static variables directly. In this case, the main method is trying to access the instance variable b directly, which is not allowed.

Therefore, the given code will result in a compilation error.

The correct answer is A) Compilation error.

Find more quizzes: