Java OCA OCP Practice Question 68

Question

Which of the following are true about the following program?

public class Main {
   public static void main(String args[]) {
      MyThread t1 = new MyThread("t1");
      MyThread t2 = new MyThread("t2");
      t1.start();//w  w w  . j  a  v  a  2  s .  com
      t2.start();
   }
}

class MyThread extends Thread {
   public void displayOutput(String s) {
      System.out.println(s);
   }

   public void run() {
      for (int i = 0; i < 10; ++i) {
         try {
            sleep((long) (3000 * Math.random()));
         } catch (Exception ex) {
         }
         displayOutput(getName());
      }
   }

   public MyThread(String s) {
      super(s);
   }

}
  • A. The program always displays t1 10 times, followed by t2 10 times.
  • B. The program always displays t1 followed by t2.
  • C. The program displays no output.
  • D. The output sequence will vary from computer to computer.


D.

Note

The output may vary because it is not synchronized.




PreviousNext

Related