Home / Java Programming / Inner Classes :: Finding the output

Java Programming :: Inner Classes

  1. What will be the output of the program?

       public class Foo
       {   
          Foo()   
         {     
            System.out.print("foo");   
         }    
        
     class Bar 
     {    
        Bar()   
       {       
           System.out.print("bar");   
       }  
       public void go() 
       {   
           System.out.print("hi");  
       }
     }  /* class Bar ends */  
      
     public static void main (String [] args)        
     {    
         Foo f = new Foo();         
         f.makeBar();   
     }    
     void makeBar()
     {      
        (new Bar() {}).go();   
     }
    }/* class Foo ends */ 

     

  2. A.

    Compilation fails.

    B.

    An error occurs at runtime.

    C.

    It prints "foobarhi"

    D.

    It prints "barhi"


  3. What will be the output of the program?

      public class HorseTest
      {    
        public static void main (String [] args)   
        {   
           class Horse      
           {         
               public String name; /* Line 7 */  
               public Horse(String s)    
               {       
                  name = s;   
               }       
            } /* class Horse ends */      
              
            Object obj = new Horse("Zippo"); /* Line 13 */ 
            Horse h = (Horse) obj; /* Line 14 */         
            System.out.println(h.name);  
        } 
    } /* class HorseTest ends */ 
    

  4. A.

    An exception occurs at runtime at line 10.

    B.

    It prints "Zippo".

    C.

    Compilation fails because of an error on line 7.

    D.

    Compilation fails because of an error on line 13.


  5. What will be the output of the program?

      public class TestObj 
      {   
        public static void main (String [] args) 
        {    
           Object o = new Object() /* Line 5 */   
           {          
               public boolean equals(Object obj)   
               {        
                   return true;       
               }  
            }      /* Line 11 */                     
           
            System.out.println(o.equals("Fred"));  
        }
     }
    

  6. A.

    It prints "true".

    B.

    It prints "Fred".

    C.

    An exception occurs at runtime.

    D.

    Compilation fails


  7. What will be the output of the program?

     public abstract class AbstractTest 
     {  
         public int getNum() 
         {    
             return 45;  
         }  
         public abstract class Bar    
         {      
            public int getNum()    
            {          
                return 38;    
            }   
         } 
         public static void main (String [] args)  
         {     
              AbstractTest t = new AbstractTest()  
              {      
                  public int getNum()         
                  {      
                      return 22;        
                  }    
              };       
              AbstractTest.Bar f = t.new Bar()    
              {     
                  public int getNum()     
                  {             
                      return 57;     
                  }     
               };              
             
               System.out.println(f.getNum() + " " + t.getNum());   
       } 
    } 
    

  8. A.

    57 22

    B.

    45 38

    C.

    45 57

    D.

    An exception occurs at runtime.