62.1k views
1 vote
What will be the result of attempting to compile the following program?

public class MyClass {
long var;
public void MyClass(long param) { var = param; } //(1)
public static void main(String[] args) {
MyClass a,b;
a = new MyClass(); //(2)
b = new MyClass(5); //(3)
}
}
(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 which takes one argument of type int
(d)The program will compile correctly
(e)The program will compile and execute correctly.

1 Answer

2 votes

Final answer:

The program will not compile correctly because the class does not have a default constructor.

Step-by-step explanation:

The correct answer is (b)A compilation error will occur at (2), since the class does not have a default constructor.

In the given program, the class MyClass has a constructor that takes a long parameter. So, when creating an object of MyClass using the statement 'a = new MyClass();', a compilation error will occur because there is no default constructor available.

To fix this error, a default constructor should be added to the class MyClass.

User Vrajesh Doshi
by
8.0k points