Java OCA OCP Practice Question 2238

Question

Which statements about the following class are correct? (Choose two.)

package mypkg; /*from ww  w  .ja v  a  2s  . c  o  m*/

class MyException extends Exception {} 

public class MyException2 extends MyException { 
   public MyException2() {  // t1 
      super(""); 
   } 
   public MyException2(String s) {  // t2 
      this(new Exception(s)); 
   } 
   public MyException2(Exception c) {  // t3 
      super(); 
   } 
   @Override public String getMessage() { 
      return "lackOf"; 
   } 
} 
  • A. MyException2 compiles without issue.
  • B. The constructor declared at line t1 does not compile.
  • C. The constructor declared at line t2 does not compile.
  • D. The constructor declared at line t3 does not compile.
  • E. The getMessage()method does not compile because of the @Override annotation.
  • F. MyException2 is a checked exception.


B, F.

Note

The MyException2 class does not compile, making Option A incorrect.

The compiler inserts the default no-argument constructor into MyException since the class does not explicitly define any.

Since MyException2 extends MyException, the only constructor available in the parent class is the no-argument call to super().

For this reason, the constructor defined at line t1 does not compile because it calls a nonexistent parent constructor that takes a String value, and Option B is one of the correct answers.

The other two constructors at lines t2 and t3 compile without issue, making Options C and D incorrect.

Option E is also incorrect.

The getMessage() method is inherited, so applying the @Override annotation is allowed by the compiler.

Option F is the other correct answer.

The MyException2 is a checked exception because it inherits Exception but not RuntimeException.




PreviousNext

Related