Java OCA OCP Practice Question 991

Question

Which statements regarding the following code are correct ?

class Base{ //from   w w w .j  a  va2 s. co  m
   void method1 () throws java.io.IOException, NullPointerException{ 
      someMethod ("arguments"); 
      // some I/O operations 
    } 
   int someMethod (String str){ 
      if (str == null) throw new NullPointerException (); 
      else return str.length (); 
    } 
} 
public class MySubClass extends Base{ 
      void method1 (){ 
           someMethod ("args"); 
       } 
} 

Select 2 options

  • A. method 1 in class MySubClass does not need to specify any exceptions.
  • B. The code will not compile because RuntimeException cannot be given in throws clause.
  • C. method 1 in class MySubClass must at least give IOException in its throws clause.
  • D. method 1 in class MySubClass must at least give NullpointerException in its throws clause. E. There is no problem with the code.


Correct Options are  : A E

Note

Overriding method only needs to specify a subset of the list of exception classes the overridden method can throw.

A set of no classes is a valid subset of that list.

Remember that NullPointerException is a subclass of RuntimeException, while IOException is a subclass of Exception.




PreviousNext

Related