Java OCA OCP Practice Question 1114

Question

What will be the result of attempting to compile the following program?

public class Main{ 
   long l1; 
   public void Main (long pLong)  { l1 = pLong ;  }  // (1) 
   public static void main (String args []){ 
      Main a, b ; 
      a = new Main ();  // (2) 
      b = new Main (5);  // (3) 
    } 
} 

Select 1 option

  • A. A compilation error will be encountered at (1), since constructors should not specify a return value.
  • B. A compilation error will be encountered at (2), since the class does not have a default constructor.
  • C. A compilation error will be encountered at (3).
  • D. The program will compile correctly.
  • E. It will not compile because parameter type of the constructor is different than the type of value passed to it.


Correct Option is  : C

Note

A. is wrong. But it becomes a valid method if you give a return type.

B. is wrong. The class has an implicit default constructor since the class doesn't have any constructor defined.

C. is correct. Because ( 1) is a method and not a constructor. So there is no constructor that take a parameter.

The declaration at (1) declares a method, not a constructor because it has a return value.

The method happens to have the same name as the class, but that is ok.

The class has an implicit default constructor since the class contains no constructor declarations. This allows the instantiation at (2) to work.




PreviousNext

Related