Discussion :: Exceptions
-
Which three statements are true?
- The default constructor initialises method variables.
- The default constructor has the same access as its class.
- The default constructor invokes the no-arg constructor of the superclass.
- If a class lacks a no-arg constructor, the compiler always creates a default constructor.
- The compiler creates a default constructor only when there are no other constructors for the class.
Answer : Option B
Explanation :
(2) sounds correct as in the example below
class CoffeeCup
{
private int innerCoffee;
public CoffeeCup()
{
}
public void add(int amount)
{
innerCoffee += amount;
}
//...
}
The compiler gives default constructors the same access level as their class. In the example above, class CoffeeCup is public, so the default constructor is public. If CoffeeCup had been given package access, the default constructor would be given package access as well.
(3) is correct. The Java compiler generates at least one instance initialisation method for every class it compiles. In the Java class file, the instance initialisation method is named "
(5) is correct. The compiler creates a default constructor if you do not declare any constructors in your class.
Be The First To Comment