Java OCA OCP Practice Question 2201

Question

Which statements are true about the following program?

public class Main {
   private static Thread t1 = new Thread("T1") {
      public void run() {
         try {/*w w w.  ja  v  a 2  s.  co m*/
            wait(1000);
         } catch (InterruptedException ie) {
         }
      }
   };

   private static Thread t2 = new Thread("T2") {
      public void run() {
         notify();
      }
   };

   private static Thread t3 = new Thread("T3") {
      public void run() {
         yield();
      }
   };

   private static Thread t4 = new Thread("T4") {
      public void run() {
         try {
            sleep(100);
         } catch (InterruptedException ie) {
         }
      }
   };

   public static void main(String[] args) {
      t1.start();
      t2.start();
      t3.start();
      t4.start();
      try {
         t4.join();
      } catch (InterruptedException ie) {
      }
   }
}

Select the three correct answers.

  • (a) The program will compile and will run and terminate normally.
  • (b) The program will compile but thread t1 will throw an exception.
  • (c) The program will compile but thread t2 will throw an exception.
  • (d) The program will compile but thread t3 will throw an exception.
  • (e) Enclosing the call to the sleep() method in a try-catch construct in thread t4 is unnecessary.
  • (f) Enclosing the call to the join() method in a try-catch construct in the main thread is necessary.


(b), (c), and (f)

Note

The wait() and notify() methods of the Object class can only be called on an object whose lock the thread holds, otherwise a java.lang.IllegalMonitorStateException is thrown.

The static method yield() of the class Thread does not throw any exceptions.

Both the sleep() and join() methods of the Thread class throw an InterruptedException.




PreviousNext

Related