Java OCA OCP Practice Question 1030

Question

What should be inserted in the code given below at line marked // 10:

class MyClass{ 
} 

class MyComparable implements Comparable<MyClass>{ 
   public int compareTo (  *INSERT CODE HERE*  x ){ //10 
         return 0; 
    } 
} 

Select 1 option

  • A. Object
  • B. MyClass
  • C. Object<MyClass>
  • D. Comparable<MyClass>
  • E. Comparable


Correct Option is  : B

Note

Since MyComparable class specifies that it implements the Comparable interface that has been typed to MyClass, it must implement compareTo() method that takes a MyClass.

Had it not declared a typed Comparable in its implements clause, compareTo(Object x) would have been correct.




PreviousNext

Related