Home / Java Programming / Threads :: Finding the output

Java Programming :: Threads

  1. What will be the output of the program?

     class Happy extends Thread 
     {    
        final StringBuffer sb1 = new StringBuffer(); 
        final StringBuffer sb2 = new StringBuffer();     
    
      public static void main(String args[])        
      {  
         final Happy h = new Happy();           
    
         new Thread()   
         {         
            public void run()          
            {                  
              synchronized(this)                  
              {                      
                 h.sb1.append("A");                      
                 h.sb2.append("B");                      
                 System.out.println(h.sb1);                      
                 System.out.println(h.sb2);                  
               }       
            }       
         }.start();     
         
         new Thread()      
         {             
             public void run()     
             {                  
                synchronized(this)                  
                {                      
                   h.sb1.append("D");                      
                   h.sb2.append("C");                      
                   System.out.println(h.sb2);                      
                   System.out.println(h.sb1);                  
                }            
             }        
          }.start();  
        } 
     } 
    

  2. A.

    ABBCAD

    B.

    ABCBCAD

    C.

    CDADACB

    D.

    Output determined by the underlying platform.


  3. class Test
    {

     public static void main(String [] args)
     {
         printAll(args);

     }
      public static void printAll(String[] lines)
      {
      for(int i = 0; i

      {
          System.out.println(lines[i]);
          Thread.currentThread().sleep(1000);
        }
      }
    }

    the static method Thread.currentThread() returns a reference to the currently executing Thread object. What is the result of this code?

     

     

  4. A.

    Each String in the array lines will output, with a 1-second pause.

    B.

    Each String in the array lines will output, with no pause in between because this method is not executed in a Thread.

    C.

    Each String in the array lines will output, and there is no guarantee there will be a pause because currentThread() may not retrieve this thread.

    D.

    This code will not compile.


  5. 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 + "..");              
             }      
         } 
     } 

     

     

     

  6. A.

    This code will not compile due to line 5.

    B.

    This code will not compile due to line 6.

    C.

    1..2..

    D.

    1..2..3..


  7. What will be the output of the program?

     class Test116 
     {  
     static final StringBuffer sb1 = new    
     StringBuffer(); 
     static final StringBuffer sb2 = new    
     StringBuffer(); 
     public static void main(String args[])    
     {
         new Thread()
         {     
             public void run()     
             {            
                synchronized(sb1)                
                {
                   sb1.append("A");                  
                   sb2.append("B");              
                 }       
               }     
     }.start();     
     
      new Thread()   
      {       
          public void run()   
           {            
               synchronized(sb1)              
               {                  
                  sb1.append("C");                  
                  sb2.append("D");                
              }        
            }     
        }.start(); /* Line 28 */      
        
        System.out.println (sb1 + " " + sb2);       
        } 
     } 
    

  8. A.

    main() will finish before starting threads.

    B.

    main() will finish in the middle of one thread.

    C.

    main() will finish after one thread.

    D.

    Cannot be determined.


  9. What will be the output of the program?

     public class ThreadTest extends Thread 
     {  
        public void run()   
        {        
            System.out.println("In run");          yield();    
             system.out.println("Leaving run");    
         }  
         public static void main(String []argv)    
          {     
             (new ThreadTest()).start();       
          }
       }
    

  10. A.

    The code fails to compile in the main() method

    B.

    The code fails to compile in the run() method

    C.

    Only the text "In run" will be displayed

    D.

    The text "In run" followed by "Leaving run" will be displayed


  11. What will be the output of the program?

    public class Test107 implements Runnable  {    
        private int x;   
        private int y;     
    
        public static void main(String args[])      
        {
           Test107 that = new Test107();          
           (new Thread(that)).start();          
           (new Thread(that)).start();  
         } 
         public synchronized void run()    
         {     
              for(int i = 0; i 10; i++)          
              {         
                   x++;           
                   y++;              
                   System.out.println("x = " + x + ", y = " + y); /* Line 17 */     
        }     
      }  
    }  
    

  12. A.

    Compilation error.

    B.

    Will print in this order: x = 1 y = 1 x = 2 y = 2 x = 3 y = 3 x = 4 y = 4 x = 5 y = 5... but the output will be produced by both threads running simultaneously.

    C.

    Will print in this order: x = 1 y = 1 x = 2 y = 2 x = 3 y = 3 x = 4 y = 4 x = 5 y = 5... but the output will be produced by first one thread then the other. This is guaranteed by the synchronised code.

    D.

    Will print in this order x = 1 y = 2 x = 3 y = 4 x = 5 y = 6 x = 7 y = 8...


  13. What will be the output of the program?

    public class Test 
     {    
        public static void main (String [] args)      
        {      
           final Foo f = new Foo();         
           Thread t = new Thread(new Runnable()      
           {       
              public void run()      
              {             
                  f.doStuff();       
              }        
           });      
           Thread g = new Thread()       
           {        
              public void run()           
              {        
                  f.doStuff();        
              }     
           };    
           t.start();      
           g.start();  
       } 
     } 
     class Foo 
     {   
           int x = 5; 
           public void doStuff()    
           {        
                if (x 10)          
                {            
                  // nothing to do               
                  try           
                  {          
                      wait();                 
                } catch(InterruptedExcepx) { }         }     
    else        
    {          
        System.out.println("x is " + x++);                
        if (x >= 10)        
        {            
                notify();     
            }        
          }     
        } 
     } 
    

  14. A.

    The code will not compile because of an error on notify(); of class Foo.

    B.

    The code will not compile because of some other error in class Test.

    C.

    An exception occurs at runtime.

    D.

    It prints "x is 5 x is 6".


  15. 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 + "..");             
                }   
             }
          } 
    

  16. A.

    Compilation fails.

    B.

    1..2..3..

    C.

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

    D.

    0..1..2..