Java OCA OCP Practice Question 1804

Question

Which digits, and in what order, will be printed when the following program is run?.

public class Main {
  public static void main(String[] args) throws InterruptedException {
    try {//from   www .  j  a v a  2 s. c  o m
      f();
      System.out.println("1");
    } finally {
      System.out.println("2");
    }
    System.out.println("3");
  }

  // InterruptedException is a direct subclass of Exception.
  static void f() throws InterruptedException {
    throw new InterruptedException("Time to go home.");
  }
}

Select the one correct answer.

  • (a) The program will print 2 and throw InterruptedException.
  • (b) The program will print 1 and 2, in that order.
  • (c) The program will print 1, 2, and 3, in that order.
  • (d) The program will print 2 and 3, in that order.
  • (e) The program will print 3 and 2, in that order.
  • (f) The program will print 1 and 3, in that order.


(a)

Note

The program will print 2 and throw an InterruptedException.

An InterruptedException is thrown in the try block.

There is no catch block to handle the exception, so it will be sent to the caller of the main() method, i.e., to the default exception handler.

Before this happens, the finally block is executed.

The code to print 3 is never reached.




PreviousNext

Related