148k views
1 vote
Consider the following statements:

Statement A: A constructor must have the same name as that of the class.
Statement B: The return type of a constructor must be void.
Select the correct option for the preceding statements:

1 Answer

3 votes

Final answer:

A constructor must have the same name as the class, but it does not have a return type.

Step-by-step explanation:

Statement A: A constructor must have the same name as that of the class.

This statement is true. In object-oriented programming, a constructor is a special method used to initialize objects of a class. As per the naming convention, a constructor must have the same name as that of the class it belongs to. This helps in identifying and creating objects of the class.

Statement B: The return type of a constructor must be void.

This statement is false. Unlike methods, constructors do not have return types, not even void. They are invoked automatically when an object of the class is created and do not return any value.

User Andras K
by
8.2k points