Java OCA OCP Practice Question 3214

Question

Consider the following program and choose the correct option describing its behavior.

public class Main {
     public static void main(String []args) throws InterruptedException {
             Thread t1 = new Thread() {
                     public void run() { System.out.print("t1 "); }
             };/* ww w .  ja  va 2 s. c o m*/
             Thread t2 = new Thread() {
                     public void run() { System.out.print("t2 "); }
             };
             t1.start();
             t1.sleep(5000);
             t2.start();
             t2.sleep(5000);
             System.out.println("main ");
     }
}
  • A. t1 t2 main
  • B. t1 main t2
  • C. main t2 t1
  • D. This program results in a compiler error.
  • E. This program throws a runtime error.


A.

Note

When a new thread is created, it is in the new state.

Then, it moves to the runnable state.

Only from the runnable state can the thread go to the timed_waiting state after calling sleep().

Hence, before executing sleep(), the run() method for that thread is called.

So, the program prints "t1 t2 main".




PreviousNext

Related