Java OCA OCP Practice Question 1308

Question

What will be the outcome when the following application is executed?

public class Main {
   public void newThread() {
      Thread t = new Thread() {
         public void run() {
            System.out.println("Going to sleep");
            try {
               sleep(5000);/*from   w ww.ja va2s  . c  o  m*/
            } catch (InterruptedException e) {
            }
            System.out.println("Waking up");
         }
      };
      t.start();
      try {
         t.join();
      } catch (InterruptedException e) {
      }
      System.out.println("All done");
   }

   public static void main(String[] args) {
      new Main().newThread();
   }
}
  • A. The code prints "Going to sleep," then "Waking up," and then "All done."
  • B. The code prints "All done," then "Going to sleep," and then "Waking up."
  • C. The code prints "All done" only.
  • D. The code prints "Going to sleep" and then "Waking up."
  • E. The code does not compile.


A.

Note

With the call to join() after the new thread is created and started, the main thread will wait for the new thread to finish its execution before continuing any processing after the call to join().




PreviousNext

Related