Type Here to Get Search Results !

What is the purpose of a constructor in a class?

0

 What is the purpose of a constructor in a class?

A) To create a new instance of a class.
B) To define methods for the class.
C) To destroy instances of a class.
D) To manage access levels for class members.

Answer: A) To create a new instance of a class.

Explanation: A constructor is a special type of method that is automatically called when an object of a class is instantiated. Its primary purpose is to initialize the object's attributes and allocate resources. Constructors can also take parameters to allow for initialization with specific values when creating an object.

Post a Comment

0 Comments