Java OCA OCP Practice Question 2660

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());
        }//from   ww  w. j ava  2  s .com
        public static void main(String args[])  {
                Thread myThread = new MyThread();
                myThread.run(); //#1
                System.out.println("In main method; thread name is: "
                         +Thread.currentThread().getName());
        }
}
a)      The program results in a compiler error at statement #1.

b)     The program results in a runtime exception.

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

d)     The program prints:// w  w  w.j a  va2s. c o  m
          In the run method; the thread name is: thread-0
          In the main method; the thread name is: main


c)

Note

The correct way to invoke a thread is to call the start() method on a Thread object.

If you directly call the run() method, the method will run just like any other method (in other words, it will execute sequentially in the same thread without running as a separate thread).




PreviousNext

Related