Java OCA OCP Practice Question 1269

Question

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

public class MyClass { 
        static int w = 1; 
        int t = 5; 
        public static void main(String[] arguments) { 
           MyClass s = new MyClass(); 
           int feet=4, t = 15; 
           System.out.print(feet + t + s.w); 
        } 
} 
  • A. The code does not compile.
  • B. 5
  • C. 10
  • D. 20


D.

Note

The code compiles and runs without issue, so Option A is incorrect.

The variables feet and t are locally scoped and set to 4 and 15, respectively.

Finally, the static variable s.w has a value of 1.

The result is the combined value is 20, making Option D the correct answer.




PreviousNext

Related