Java OCA OCP Practice Question 1789

Question

Which expression, when inserted into the blank in the following class, allows the code to compile?

package mypkg;/*from ww  w  . j av  a  2  s.co  m*/
public class Main {
   class Login implements AutoCloseable {
      @Override public void close() throws Exception1 {}
   }
   class Exception1 extends Exception {
      public Exception1(String message) {}
   }
   public static void main(String[] notes) throws Throwable {
      try (Login p = null) {
         throw new Exception();
      } catch (Exception e) {
      } catch (                         ) {
      }
   }
}
  • A. Error r
  • B. IllegalStateException b
  • C. Exception1 p
  • D. The code does not compile regardless of the expression used.


A.

Note

The try-catch block already catches Exception, so the correct answer would be the one that is not a subclass of Exception.

In this case, Error extends Throwable and is the only choice that allows the code to compile.

Because IllegalStateException and Exception1 both inherit from Exception, Options B and C, respectively, are incorrect.

Finally, Option D is incorrect because there is an answer choice that allows the code to compile.




PreviousNext

Related