Java OCA OCP Practice Question 311

Question

What is the output of the following?

package beach; //from  www .  j a  va  2 s.c o m
public class MyClass { 
   public MyClass() { 
      System.out.print("a"); 
   } 
   public void MyClass() { 
      System.out.print("b"); 
   } 
   public void run() { 
      new MyClass(); 
      MyClass(); 
   } 
   public static void main(String... args) { 
      new MyClass().run(); 
   } 
} 
A.  a 
B.  ab 
C.  aab 
D.  None of the above 


C.

Note

The main() method calls the constructor which outputs a.

Then the main method calls the run() method.

The run() method calls the constructor again, which outputs a again.

Then the run() method calls the MyClass() method, which happens to have the same name as the constructor. This outputs b. Therefore, Option C is correct.




PreviousNext

Related