Java OCA OCP Practice Question 2662

Question

Consider the following program and predict the output:

class MyThread extends Thread {
        public void run() {
                System.out.println("In run method; thread name is: " + Thread.
                currentThread().getName());
        }//  w w  w. j  av a 2 s  .c  o  m
        public static void main(String args[])  {
                Thread myThread = new MyThread();
                myThread.start();
                myThread.start();  //#1
        }
}
a)      The program results in a compiler error at statement #1.

b)     The program results in throwing an IllegalThreadStateException.

c)      The program prints the following:
          In the run method; thread name is: thread-0
          In the main method; thread name is: thread-0

d)     The program prints the following:
         In the run method; thread name is: thread-0


b)

Note

If you invoke the start() method on a thread object twice, it will result in a IllegalThreadStateException.




PreviousNext

Related