Multiple choice

When attempting to compile and run the following code, the result will be

public class MyClass extends Thread{
       public MyClass(String s){ msg=s; }
       String msg;
       public void run(){
             System.out.println(msg);
       }
       public static void main(String[] args) {
             new MyClass(Hello);
             new MyClass(World);
       }
}

  1. The program will fail to compile.

  2. The program will compile without any errors and will print 'Hello' and 'World' everytime the program is run.

  3. The program will compile without errors and will print 'Hello' and 'World' but the order is unpredictable.

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

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

The program will compile without errors and will simply terminate without any output. Two threads are 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.