Java OCA OCP Practice Question 98

Question

What is the output of the following application?

public class MyClass { 
   static int start = 2; 
   final int end; 
   public MyClass(int x) { 
      x = 4; /*from   ww  w. j  av a  2 s .c o  m*/
      end = x; 
   } 
   public void fly(int distance) { 
      System.out.print(end-start+" "); 
      System.out.print(distance); 
   } 
   public static void main(String... start) { 
      new MyClass(10).fly(5); 
   } 
} 
  • A. 2 5
  • B. 8 5
  • C. 6 5
  • D. The code does not compile.


A.

Note

The code compiles so Option D is incorrect.

The input to the constructor is ignored, making the assignment of end to be 4.

Since start is 2, the subtraction of 4 by 2 results in the application printing 2, followed by 5, making Option A the correct answer.




PreviousNext

Related