Java OCA OCP Practice Question 71

Question

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

1: public class MyClass { 
2:    private static int yesterday = 1; 
3:    int tomorrow = 10; 
4:    public static void main(String[] args) { 
5:       MyClass tolls = new MyClass(); 
6:       int myValue=20, tomorrow = 40; 
7:       System.out.print(myValue + tolls.tomorrow + tolls.yesterday); 
8:    } 
9: } 
  • A. The code does not compile due to line 6.
  • B. The code does not compile due to line 7.
  • C. 31
  • D. 61


C.

Note

The code compiles and runs without issue, so Options A and B are incorrect.

The question tests your understand on variable scope.

The variable myValue has local scope to the method in which it is executed.

The variable tomorrow is re-declared in the method, but the reference used on line 7 is to the instance variable with a value of 10.

Finally, the variable tomorrow is static.

While using an instance reference to access a static variable is not recommended, it does not prevent the variable from being read.

The result is line 7 evaluates and prints (20 + 10 + 1) = 31, making C the correct answer.




PreviousNext

Related