Java OCA OCP Practice Question 2826

Question

Which of the following can legally fill in the blank? (Choose all that apply.)

public class Main { 
   static class Exception1 extends Exception { } 
   static class Exception2 extends Exception1 { } 
   public static void main(String[] args) throws Exception1 { 
      try { /*from   ww w .  j  av  a  2  s .  c  o m*/
         throw new Exception1(); 
      } catch (Exception1 e) { 
           ______________ 
           throw e; 
      } 
   } 
} 
  • A. // leave line blank
  • B. e = new Exception();
  • C. e = new RuntimeException();
  • D. e = new Exception1();
  • E. e = new Exception2();
  • F. None of the above; the code does not compile.


A, D, E.

Note

Since a single exception type is caught, only the same type of exception or a subclass is allowed to be assigned to the variable in the catch block.

Therefore D and E are correct.

Additionally A is correct because there are no changes to the variable.




PreviousNext

Related