Java OCA OCP Practice Question 44

Question

What is the result of compiling and executing the following class?

1: public class MyClass { 
2:    int birds = 10; 
3:    public static void main(String[] data) { 
4:       int trees = 5; 
5:       System.out.print(trees + birds); 
6:    } 
7: } 
  • A. It does not compile.
  • B. It compiles but throws an exception at runtime.
  • C. It compiles and outputs 5.
  • D. It compiles and outputs 15.


A.

Note

The code does not compile because of line 5, making Option A the correct answer.

The main() method is static and does not have access to any class instance variables.

The birds variable is not static and requires a class instance variable to access.

The code does not compile when the static method attempts to access a non-static variable without an instance of the class.




PreviousNext

Related