Multiple choice

What will be the output of the program? class MyThread extends Thread { public static void main(String [] args) { MyThread t = new MyThread(); /* Line 5 / t.run(); / Line 6 */ }

public void run() { for(int i=1; i < 3; ++i) { System.out.print(i + ..); } } }

  1. This code will not compile due to line 5.

  2. This code will not compile due to line 6.

  3. 1..2..

  4. 1..2..3..

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

Line 6 calls the run() method, so the run() method executes as a normal method should and it prints 1..2.. Option 1 is incorrect because line 5 is the proper way to create an object. Option 2 is incorrect because it is legal to call the run() method, even though this will not start a true thread of execution. The code after line 6 will not execute until the run() method is complete. Option 4 is incorrect because the for loop only does two iterations.