Java OCA OCP Practice Question 1596

Question

Which line contains a valid constructor in the following class definition?

public class Main{
   int i, j;
   public Main getInstance ()  {  return new Main ();     }  //1
   public void Main (int x, int y)  {   i = x;   j = y;    }     //2
   public Main Main ()  {   return new Main ();    }    //3
   public ~Main ()  {}                     //4
}

Select 1 option

  • A. Line 1
  • B. Line 2
  • C. Line 3
  • D. Line 4
  • E. None of the above.


Correct Option is  : E

Note

For A.

This cannot be a constructor because even the name of the method (getInstance) is not same as the class name!

For B.

Constructors cannot return anything. Not even void.

for C.

Constructors cannot return anything. Not even void.

For D.

This could have been a destructor in C++ world.

And there nothing like this in java.

Java has a finalize() method, which is similar to a destructor but does not work exactly as a destructor.




PreviousNext

Related