Java OCA OCP Practice Question 1100

Question

Which lines contain a valid constructor in the following code?

public class Main{ 
   public Main (int a, int b)  {  } // 1 
   public void Main (int a)  {  }   // 2 
   public Main (String s); // 3 
   private Main (String s, int a)  {  }     //4 
   public Main (String s1, String s2)  {  }; //5 
} 

Select 3 options

  • A. Line // 1
  • B. Line // 2
  • C. Line // 3
  • D. Line // 4
  • E. Line // 5


Correct Options are  : A D E

Note

Constructors cannot return anything. Not even void.

Constructors cannot have empty bodies (i.e. they cannot be abstract)

You can apply public, private, protected to a constructor.

But not static, final, synchronized, native and abstract.

The compiler ignores the extra semi-colon.

It is interesting to note that public void Main (int a) {} // 2 will actually compile.

It is not a constructor, but compiler considers it as a valid method!




PreviousNext

Related