C++ Constructors and Destructors MCQ

Constructors and destructors are fundamental concepts in object-oriented programming, especially in C++. They play a crucial role in the initialization and cleanup of objects. Let's see how well you understand these concepts with this beginner-friendly quiz!

1. Which of the following is true about constructors in C++?

a) Used to allocate memory
b) Used only to initialize static variables
c) Always have the same name as the class
d) Used to delete objects

Answer:

c) Always have the same name as the class

Explanation:

Constructors have the same name as the class and are used to initialize object properties when an object is created.

2. How many constructors can a class have in C++?

a) Only 1
b) 2
c) 3
d) As many as needed

Answer:

d) As many as needed

Explanation:

A class in C++ can have multiple constructors as long as they have different parameters. This is known as constructor overloading.

3. Which constructor gets automatically added to a class if we don't add any constructor?

a) Parameterized constructor
b) Copy constructor
c) Default constructor
d) None of the above

Answer:

c) Default constructor

Explanation:

If no constructor is provided, the compiler automatically provides a default constructor.

4. What does the destructor do in C++?

a) Initializes an object
b) Deletes the object
c) Releases the memory occupied by the object
d) Copies the object

Answer:

c) Releases the memory occupied by the object

Explanation:

The destructor is called automatically when an object goes out of scope. It is used to release any resources (like memory) that the object might have acquired during its lifetime.

5. What is the name of the destructor for a class named Book?

a) Book()
b) ~Book()
c) BookDestruct()
d) DeleteBook()

Answer:

b) Book()

Explanation:

The destructor has the same name as the class but is preceded by a tilde ().

6. Which of the following statements is true?

a) Constructors return values.
b) Destructors have parameters.
c) Destructors return values.
d) Constructors can be overloaded.

Answer:

d) Constructors can be overloaded.

Explanation:

Constructors can be overloaded based on the number and/or type of parameters. Destructors don't have parameters and neither constructors nor destructors return values.

7. Which type of constructor is called when an object is created as a copy of another?

a) Default constructor
b) Parameterized constructor
c) Copy constructor
d) Static constructor

Answer:

c) Copy constructor

Explanation:

The copy constructor initializes an object as a copy of another. It typically takes a reference to the same class as its parameter.

8. When is the destructor called?

a) When the object is created
b) Just before the object is deleted
c) After the constructor is executed
d) When the program starts

Answer:

b) Just before the object is deleted

Explanation:

The destructor is called just before the object's memory is deallocated, i.e., when the object is about to be deleted.

9. Can destructors be overloaded in C++?

a) Yes
b) No

Answer:

b) No

Explanation:

Destructors cannot be overloaded. A class can have only one destructor.

10. Which of the following is the correct way to define a parameterized constructor for a class named Student that takes an integer age?

a) Student(int age) { }
b) int Student::Student(int age) { }
c) void Student(int age) { }
d) Student::Student() { }

Answer:

a) Student(int age) { }

Explanation:

The correct way to define a parameterized constructor is to give it the same name as the class and specify the parameters.



Comments