Home / Java Programming / Inner Classes :: Discussion

Discussion :: Inner Classes

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

  2. A.

    It prints "true".

    B.

    It prints "Fred".

    C.

    An exception occurs at runtime.

    D.

    Compilation fails

    View Answer

    Workspace

    Answer : Option D

    Explanation :

    This code would be legal if line 11 ended with a semicolon. Remember that line 5 is a statement that doesn't end until line 11, and a statement needs a closing semicolon!


Be The First To Comment