Java OCA OCP Practice Question 2403

Question

Select the incorrect options:

  • a If a user defines a private constructor for a public class, Java creates a public default constructor for the class.
  • b A class that gets a default constructor doesn't have overloaded constructors.
  • c A user can overload the default constructor of a class.
  • d The following class is eligible for a default constructor:
class Main {} 

e  The following class is also eligible for a default constructor: 

class Main { 
    void Main() {} 
} 


a, c

Note

Option (a) is incorrect.

If a user defines a constructor for a class with any access modifier, it's no longer an eligible candidate to be provided with a default constructor.

Option (b) is correct.

A class gets a default constructor only when it doesn't have any constructor.

A default or an automatic constructor can't exist with other constructors.

Option (c) is incorrect.

A default constructor can't coexist with other constructors.

A default constructor is automatically created by the Java compiler if the user doesn't define any constructor in a class.

If the user reopens the source code file and adds a constructor to the class, upon recompilation no default constructor will be created for the class.

Option (d) is correct.

Because this class doesn't have a constructor, Java will create a default constructor for it.

Option (e) is also correct.

This class also doesn't have a constructor, so it's eligible for the creation of a default constructor.

The following isn't a constructor because the return type of a constructor isn't void:.

void Main() {} 

It's a regular and valid method, with the same name as its class.




PreviousNext

Related