Java OCA OCP Practice Question 3196

Question

Consider the following class hierarchy from the package java.nio.file and answer the question.


Exception/*from w w w  . ja  v a2  s. co m*/
  +
  |
  IOException
      +
      |
      FileSystemException
            +
            |
            AccessDeniedException                    
            +    +
            |    |
            |    AtomicMoveNotSupportedException
            |
            |            
            DirectoryNotSupportedException
                +
                |
                FileAlreadyExistsException

In the following class definitions, the base class Base has the method foo() that throws a FileSystemException; the derived class Deri extending the class Base overrides the foo() definition.

class Base {
       public void foo() throws FileSystemException {
               throw new FileSystemException("");
       }
}

class Deri extends Base {
       /* provide foo definition here */
}

Which of the following overriding definitions of the foo() method in the Deri class are compatible with the base class foo() method definition?

Choose all the foo() method definitions that could compile without errors when put in the place of the comment: /* provide foo definition here */.

A.
   public void foo() throws IOException {
           super.foo();
  }//w  w w.  j  a  v  a  2s .com


B.
 public void foo() throws AccessDeniedException {
          throw new AccessDeniedException("");
 }

C.
  public void foo() throws FileSystemException, RuntimeException {
           throw new NullPointerException();
  }

D.
  public void foo() throws Exception {
           throw new NullPointerException();
  }


B and C.

Note

In option A and D, the throws clause declares to throw exceptions IOException and Exception respectively, which are more general than the FileSystemException, so they are not compatible with the base method definition.

In option B, the foo() method declares to throw AccessDeniedException, which is more specific than FileSystemException, so it is compatible with the base definition of the foo() method.

In option C, the throws clause declares to throw FileSystemException, which is the same as in the base definition of the foo() method.

Additionally it declares to throw RuntimeException, which is not a checked exception, so the definition of the foo() method is compatible with the base definition of the foo() method.




PreviousNext

Related