Java OCA OCP Practice Question 2187

Question

What will be the result of attempting to compile and run the following program?

public class Worker extends Thread {
  public void run() {
    System.out.print("|work|");
  }/*w  w  w.j  a v a 2  s .c  om*/
  public static void main(String[] args) {
    Worker worker = new Worker();
    worker.start();
    worker.run();
    worker.start();
  }
}

    

Select the one correct answer.

  • (a) The program will fail to compile.
  • (b) The program will compile without errors, will print |work| twice, and terminate normally every time the program is run.
  • (c) The program will compile without errors, will print|work| three times, and terminate normally every time the program is run.
  • (d) The program will compile without errors, will print|work| twice, and throw an IllegalStateException, every time the program is run.
  • (e) None of the above.


(d)

Note

The call to the run() method just executes the method in the main thread.

Once a thread has terminated, it cannot be started by calling the start() method as shown above.

A new thread must be created and started.




PreviousNext

Related