Java OCA OCP Practice Question 1867

Question

What will the following program print when run?.

public class Main{
  public static void main (String [] args){
     try{//from www.j av  a2 s  .  com
        System.exit (0);
     }
     finally{
         System.out.println ("finally is always executed !");
     }
   }
}

Select 1 option

  • A. It will print "finally is always executed!"
  • B. It will not compile as there is no catch block.
  • C. It will not print anything.
  • D. An exception will be thrown
  • E. None of the above.


Correct Option is  : C

Note

finally is always executed (even if you throw an exception in try or catch) but this is the exception to the rule.

When you call System.exit(...); The JVM exits so there is no way to execute the finally block.




PreviousNext

Related