Java Programming - Threads - Discussion

Discussion Forum : Threads - Finding the output (Q.No. 13)
What will be the output of the program?
class MyThread extends Thread 
    public static void main(String [] args) 
        MyThread t = new MyThread(); /* Line 5 */;  /* Line 6 */

    public void run() 
        for(int i=1; i < 3; ++i) 
            System.out.print(i + "..");
This code will not compile due to line 5.
This code will not compile due to line 6.
Answer: Option

Line 6 calls the run() method, so the run() method executes as a normal method should and it prints "1..2.."

A is incorrect because line 5 is the proper way to create an object.

B 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.

D is incorrect because the for loop only does two iterations.

3 comments Page 1 of 1.

Nikhil said:   2 years ago

i++ and ++i is same if we call it as a statement ending with ';'

Stefan said:   7 years ago
Doesn't ++i in the for loop suggest i is incremented to value 2 before the print operation calls?

Risha said:   10 years ago
It shows an error: Error - At least one public class is required in main file.

Post your comments here:

Your comments will be displayed after verification.