Java OCA OCP Practice Question 3138

Question

Consider the following program:

public class Main extends Thread {
        public static void main(String[] args) {
               new Main().start();
        }/*from w ww .j  a v a  2 s.  co m*/
        public void run() {
               try {
                       System.out.println("Starting to wait");
                       wait(1000);
                       System.out.println("Done waiting, returning back");
               }
               catch(InterruptedException e) {
                       System.out.println("Caught InterruptedException ");
               }
               catch(Exception e) {
                       System.out.println("Caught Exception ");
               }
        }
}

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

a) The program prints// w  w w .  j  a v a  2s  . co m
     Starting to wait
     Done waiting, returning back
b) The program prints
     Starting to wait
     Caught InterruptedException
c) The program prints
     Starting to wait
     Caught Exception
d) The program prints
     Starting to wait
     After that, the program gets into an infinite wait and deadlocks


c)

Note

In this program, the wait() method is called without acquiring a lock; hence it will result in throwing an IllegalMonitorStateException, which will be caught in the catch block for the Exception.




PreviousNext

Related