Java OCA OCP Practice Question 1632

Question

What will be the result of compiling the following program?.

public class Main {
  long var;

  public void Main(long param) { var = param; }  // (1)

  public static void main(String[] args) {
    Main a, b;/*  w w w  .ja  v a2s .  c o m*/
    a = new Main();                              // (2)
    b = new Main(5);                             // (3)
  }
 }

Select the one correct answer.

  • (a) A compilation error will occur at (1), since constructors cannot specify a return value.
  • (b) A compilation error will occur at (2), since the class does not have a default constructor.
  • (c) A compilation error will occur at (3), since the class does not have a constructor that takes one argument of type int.
  • (d) The program will compile without errors.


(c)

Note

A compilation error will occur at (3), since the class does not have a constructor accepting a single argument of type int.

The declaration at (1) declares a method, not a constructor, since it is declared as void.

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

The class has an implicit default constructor, since the class contains no constructor declarations.

This constructor is invoked to create a Main object at (2).




PreviousNext

Related