Java OCA OCP Practice Question 3122

Question

Consider the following program:

public class Main {
        public static void main(String []args) {
               try {
                        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 -> ");
                }//w  ww .j  a  v  a2  s  . co m
                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 -> in finally -> after everything.
  • d) The program prints the following: in catch -> after everything.
  • e) The program prints the following: in catch -> in finally ->.
  • f) When compiled, the program results in a compiler error in line marked with comment in LINE A for divide-by-zero.


e)

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