Java OCA OCP Practice Question 2176

Question

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

public class Main extends Thread {
  public Main(String s) { msg = s; }
  String msg;//from   w  w w  .j av a 2 s .  co  m
  public void run() {
    System.out.println(msg);
  }

  public static void main(String[] args) {
    new Main("Hello");
    new Main("World");
  }
}

    

Select the one correct answer.

  • (a) The program will fail to compile.
  • (b) The program will compile without errors and will print Hello and World, in that order, every time the program is run.
  • (c) The program will compile without errors and will print a never-ending stream of Hello and World.
  • (d) The program will compile without errors and will print Hello and World when run, but the order is unpredictable.
  • (e) The program will compile without errors and will simply terminate without any output when run.


(e)

Note

The program will compile without errors and will simply terminate without any output when run.

Two thread objects will be created, but they will never be started.

The start() method must be called on the thread objects to make the threads execute the run() method asynchronously.




PreviousNext

Related