Java OCA OCP Practice Question 2637

Question

Given that FileNotFoundException extends IOException, and given:

2. import java.io.*;  
3. public class Main extends Printable {  
4.   public static void main(String[] args) {  
5.     new Main().print();  
6.   }  /*from   ww w.  ja  v  a  2s.co  m*/
7.   // insert code here  
8. }  
9. class Printable {  
10.   void print() throws IOException { }  
11. } 

Which method(s), inserted independently at line 7, compile? (Choose all that apply.)

  • A. void print() { }
  • B. void print() throws Exception { }
  • C. void print(int x) throws Exception { }
  • D. void print() throws RuntimeException { }
  • E. void print() throws FileNotFoundException { }


H:Note

A and D are correct.

It's legal for an overriding method to throw fewer exceptions, and runtime exceptions are in a class hierarchy separate from checked exceptions.

C would be correct if the invocation on line 5 was either handled or declared because it's not an override.

B is wrong because it's a broader exception.

E would be correct if the invocation on line 5 was either handled or declared.




PreviousNext

Related