Java OCA OCP Practice Question 2667

Question

Given:

2. import java.io.*;  
3. interface Printable {  
4.   String m() throws Exception;  
5.   Printable get();  //from w  w  w. j a  v  a  2s. c o m
6.   void doInsane();  
7. }  
8. class Shape implements Printable {  
9.   public String m() throws IOException {   
10.     throw new IOException();   
11.   }  
12.   public Shape get() { return new Shape(); }   
13.   public void doInsane() throws NullPointerException {  
14.     throw new NullPointerException();   
15.   } 
16. } 

What is the result? (Choose all that apply.)

  • A. Compilation succeeds.
  • B. The Printable interface will not compile.
  • C. The Shape.m() method will not compile.
  • D. The Shape.get() method will not compile.
  • E. The Shape.doInsane() method will not compile.


A is correct, the code is all legal.

Note

It's okay for an interface method to declare an exception.

It's okay for an implementing method to throw either a narrower exception or runtime exception.

Finally, it's okay for an implementing method to use a covariant return (as in line 12).




PreviousNext

Related