What is the output from the following program? : Questions « Thread « SCJP






public class MainClass {
  public static void main(String args[]) {
    MyThread t1 = new MyThread("t1");
    MyThread t2 = new MyThread("t2");
    t1.start();
    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);
  }
}








7.14.Questions
7.14.1.A dead thread cannot be restarted(True/False).
7.14.2.Answer: restarting a thread
7.14.3.The yield() method puts a thread in the waiting state(True/False).
7.14.4.Answer: yield method
7.14.5.Only threads have locks(True/False).
7.14.6.Answer: threads and locks
7.14.7.Classes have locks(True/False).
7.14.8.Answer: classes and locks
7.14.9.Primitive types have locks(True/False).
7.14.10.Answer: primitive and locks
7.14.11.Only Runnable objects have locks(True/False).
7.14.12.Answer: runnable objects and locks
7.14.13.The Thread class inherits the wait() and notify() methods(True/False).
7.14.14.Answer: wait() and notify() method
7.14.15.The Object class declares the wait() and notify() methods(True/False).
7.14.16.Answer: Object class and wait() and notify() method
7.14.17.Only the Synchronized class has the wait() and notify() methods(True/False).
7.14.18.Answer: synchronized class
7.14.19.What is the output from the following programm?
7.14.20.Answer: running thread
7.14.21.What is the output from the following program?
7.14.22.Answer: random sleep