Java OCA OCP Practice Question 2913

Question

Given:

public class Main extends Thread {
   public static void main(String[] args) {
      try {//from   www.j  av  a 2 s. c o m
         Thread t = new Thread(new Main());
         Thread t2 = new Thread(new Main());
      } catch (Exception e) {
         System.out.print("e ");
      }
   }

   public void run() {
      for (int i = 0; i < 2; i++) {
         try {
            Thread.sleep(500);
         } catch (Exception e) {
            System.out.print("e2 ");
         }
         System.out.print(Thread.currentThread().getName() + " ");
      }
   }
}

Which are true? (Choose all that apply.)

  • A. Compilation fails.
  • B. No output is produced.
  • C. The output could be Thread-1 Thread-1 e
  • D. The output could be Thread-1 Thread-1 Thread-3 Thread-3
  • E. The output could be Thread-1 Thread-3 Thread-1 Thread-3
  • F. The output could be Thread-1 Thread-1 Thread-3 Thread-2


B is correct.

Note

In main(), the start() method was never called to start "t" and "t2", so run() never ran.

If start() was invoked for "t" and "t2", then D and E would have been correct.




PreviousNext

Related