Java OCA OCP Practice Question 849

Question

What is wrong with the following code written in a single file named Main.java?

class Exception1 extends Throwable  {  } 
class Exception2 extends Exception1  {  } 
public class Main{ 
   public static void main (String args []) throws Exception1{ 
      try{ /* ww w  .j  a  v a  2 s .c  o  m*/
         m1 (); 
       }catch (Exception1 e){ 
         throw e; 
       }finally{ 
         System .out.println ("Done"); 
       } 
    } 
   public static void m1 () throws Exception2{ 
      throw new Exception2 (); 
    } 
} 

Select 2 options

  • A. The main declares that it throws Exception1 but throws Exception2.
  • B. You cannot have more than 2 classes in one file.
  • C. The catch block in the main method must declare that it catches Exception2 rather than Exception1.
  • D. There is nothing wrong with the code.
  • E. Done will be printed.


Correct Options are  : D E

Note

For Option A.

That's OK. You can put a Super class in the throws clause and then you can throw any subclass exception.

For Option B.

You sure can.

The only limitation is you can have only one top level public class in a file.

For Option C.

You can catch a subclass exception in the catch clause that catches a super class.

for Option E.

Done will be followed by an exception. Finally is always executed and Only exception is System .exit ();




PreviousNext

Related