Java OCA OCP Practice Question 1337

Question

Given that EOFException is a subclass of IOException, what is the output of the following application?

package ai; //  w  w w  .  j  a va2s  . c  om

import java.io.*; 

class Shape { 
   public boolean hasArea() throws EOFException {return true;} 
} 

public class Rectangle extends Shape { 
   public boolean hasArea() throws IOException {return false;} 
   public static void main(String[] doesNotCompute) throws Exception { 
      Shape m = new Rectangle(); 
      System.out.print(m.hasArea()); 
   } 
} 
  • A. true
  • B. false
  • C. The code does not compile.
  • D. The code compiles but produces an exception at runtime.


C.

Note

For overriding a method with exceptions:

the subclass cannot throw any new or broader checked exceptions.

Since IOException is a superclass of EOFException,

this is a broader exception and therefore not compatible.

For this reason, the code does not compile, and Option C is the correct answer.




PreviousNext

Related