Home / Java Programming / Threads :: Discussion

Discussion :: Threads

  1. What will be the output of the program?

       class MyThread extends Thread
       {  
          public static void main(String args)      
          {      
              MyThread t = new MyThread();           
              Thread x = new Thread(t);         
              x.start(); /* Line 7 */  
            }   
            public void run()   
            {        
                for(int i = 0; i 3; ++i)          
                {             
                   System.out.print(i + "..");             
                }   
             }
          } 
    

  2. A.

    Compilation fails.

    B.

    1..2..3..

    C.

    0..1..2..3..

    D.

    0..1..2..

    View Answer

    Workspace

    Answer : Option D

    Explanation :

    The thread MyThread will start and loop three times (from 0 to 2).

    Option A is incorrect because the Thread class implements the Runnable interface; therefore, in line 7, Thread can take an object of type Thread as an argument in the constructor.

    Option B and C are incorrect because the variable i in the for loop starts with a value of 0 and ends with a value of 2.


Be The First To Comment