Java OCA OCP Practice Question 1116

Question

Given the following code, what will be the outcome?

public class MyClass extends java.lang.Math { 
   public int add(int x, int y) { 
      return x + y; 
   } //from   w ww  .j av a2  s.c  om
   public int sub(int x, int y) { 
      return x - y; 
   } 
   public static void main(String [] a) { 
      MyClass f = new MyClass(); 
      System.out.println("" + f.add(1, 2)); 
   } 
} 
  • A. The code compiles but does not output anything.
  • B. "3" is printed out to the console.
  • C. The code does not compile.
  • D. None of the above.


C.

Note

The code does not compile because it extends the Math class.

Math class has been declared as final.

A class cannot extend a class that has been declared final.




PreviousNext

Related