Java OCA OCP Practice Question 2237

Question

Consider the following program:

public class Main {
     public static void main(String []args) {
         try {/*www  . java  2s.co m*/
             int i = 10/0; // LINE A
             System.out.print("after throw -> ");
         } catch(ArithmeticException ae) {
             System.out.print("in catch -> ");
             return;
         } finally {
             System.out.print("in finally -> ");
     }
     System.out.print("after everything");
   }
}

Which one of the following options best describes the behavior of this program?

  • A. the program prints the following: in catch -> in finally -> after everything
  • B. the program prints the following: after throw -> in catch -> in finally -> after everything
  • C. the program prints the following: in catch -> after everything
  • d. the program prints the following: in catch -> in finally ->
  • e. When compiled, the program results in a compiler error in line marked with comment in Line a for divide-by-zero


d.

Note

the statement println("after throw -> "); will never be executed since the line marked with the comment Line a throws an exception.

the catch handles ArithmeticException, so println("in catch -> "); will be executed.

Following that, there is a return statement, so the function returns.

But before the function returns, the finally statement should be called, hence the statement println("in finally -> "); will get executed.

so, the statement println("after everything"); will never get executed.




PreviousNext

Related