C++ Constructors and Destructors MCQs

1 min read

In C++, constructors and destructors are special member functions critical for the Resource Acquisition Is Initialization (RAII) paradigm and safe memory management. A constructor automatically initializes an object when it is instantiated, allowing for default, parameterized, and copy variations to establish an object’s initial state. Conversely, a destructor is invoked automatically when an object goes out of scope or is explicitly deleted, ensuring that dynamically allocated memory and system resources are safely released. Advanced C++ programming relies heavily on constructor initializer lists for const or reference members, and virtual destructors to prevent memory leaks during polymorphic base-class deletion. Understanding the strict execution hierarchy—where base class constructors execute before derived ones, and destructors execute in the exact reverse order—is essential for robust object-oriented software design.

1. What is the main purpose of a constructor?

a) Initialize objects
b) Delete objects
c) Allocate globals
d) Define namespaces
Correct Answer: a) Initialize objects
Explanation:
Constructors set up initial state when objects are created.

2. What is the name of a constructor?

a) main
b) The class name
c) init
d) constructor
Correct Answer: b) The class name
Explanation:
A constructor has the same name as its class.

3. What is the return type of a constructor?

a) int
b) void
c) It has no return type
d) bool
Correct Answer: c) It has no return type
Explanation:
Constructors do not declare return types.

4. When is a constructor called?

a) At object destruction
b) At class definition
c) At program parsing
d) At object creation
Correct Answer: d) At object creation
Explanation:
Constructors run when an object is created.

5. What is a default constructor?

a) A constructor that can be called with no arguments
b) A constructor with one required argument
c) A deleted constructor
d) A virtual constructor
Correct Answer: a) A constructor that can be called with no arguments
Explanation:
Default constructors create objects without requiring arguments.

6. What is a parameterized constructor?

a) A constructor with no parameters
b) A constructor that takes arguments
c) A destructor
d) A static function
Correct Answer: b) A constructor that takes arguments
Explanation:
Parameterized constructors initialize objects using supplied values.

7. What does a copy constructor do?

a) Deletes an object
b) Assigns to an existing object
c) Initializes an object from another object of the same class
d) Converts to int
Correct Answer: c) Initializes an object from another object of the same class
Explanation:
Copy constructors create new objects as copies.

8. What does a move constructor do?

a) Copies all data slowly
b) Prevents object creation
c) Calls the destructor
d) Transfers resources from an rvalue source
Correct Answer: d) Transfers resources from an rvalue source
Explanation:
Move constructors avoid deep copies by transferring ownership.

9. What is the name of a destructor for class MyClass?

a) ~MyClass
b) MyClass
c) deleteMyClass
d) destroyMyClass
Correct Answer: a) ~MyClass
Explanation:
Destructors use a tilde before the class name.

10. What is the return type of a destructor?

a) int
b) It has no return type
c) void
d) bool
Correct Answer: b) It has no return type
Explanation:
Destructors do not declare return types.

11. How many parameters can a destructor have?

a) One
b) Two
c) None
d) Unlimited
Correct Answer: c) None
Explanation:
Destructors cannot take arguments.

12. When is a destructor called?

a) When a class is declared
b) When an object is copied
c) When a constructor throws
d) When an object is destroyed
Correct Answer: d) When an object is destroyed
Explanation:
Destructors clean up objects at end of lifetime.

13. Can a constructor be private?

a) Yes
b) No
c) Only in structs
d) Only if virtual
Correct Answer: a) Yes
Explanation:
Private constructors restrict object creation.

14. Can a destructor be private?

a) No
b) Yes
c) Only in unions
d) Only if static
Correct Answer: b) Yes
Explanation:
Private destructors restrict deletion or destruction.

15. Can constructors be overloaded?

a) No
b) Only copy constructors
c) Yes
d) Only destructors
Correct Answer: c) Yes
Explanation:
A class can have multiple constructors with different signatures.

16. Can destructors be overloaded?

a) Yes
b) Only in derived classes
c) Only if virtual
d) No
Correct Answer: d) No
Explanation:
A class has only one destructor with no parameters.

17. Can a constructor be virtual?

a) No
b) Yes
c) Only in abstract classes
d) Only if pure
Correct Answer: a) No
Explanation:
Constructors cannot be virtual.

18. Can a destructor be virtual?

a) No
b) Yes
c) Only private
d) Only static
Correct Answer: b) Yes
Explanation:
Virtual destructors support safe polymorphic deletion.

19. Can a constructor be static?

a) Yes
b) Only in templates
c) No
d) Only in structs
Correct Answer: c) No
Explanation:
Constructors are associated with object initialization, not static dispatch.

20. Can a destructor be static?

a) Yes
b) Only if virtual
c) Only in unions
d) No
Correct Answer: d) No
Explanation:
Destructors operate on objects and cannot be static.

21. Can a constructor be const?

a) No
b) Yes
c) Only in C++20
d) Only for copy constructors
Correct Answer: a) No
Explanation:
Constructors initialize objects and are not const-qualified.

22. Can member functions be called inside a constructor?

a) Never
b) Yes
c) Only destructors
d) Only static functions
Correct Answer: b) Yes
Explanation:
Member functions can be called, but object state may still be incomplete.

23. Can a destructor be called explicitly?

a) No
b) Only through delete
c) Yes, using syntax such as obj.~MyClass()
d) Only by compiler
Correct Answer: c) Yes, using syntax such as obj.~MyClass()
Explanation:
Explicit destructor calls are allowed but usually avoided.

24. Should you usually call a destructor explicitly?

a) Always
b) Only for globals
c) Yes, for all objects
d) No, unless using special techniques like placement new
Correct Answer: d) No, unless using special techniques like placement new
Explanation:
Automatic and delete-based cleanup normally calls destructors.

25. What happens if a class declares no constructor?

a) The compiler may declare a default constructor
b) The class cannot be used
c) A destructor is not generated
d) All members become private
Correct Answer: a) The compiler may declare a default constructor
Explanation:
An implicit default constructor may be declared when needed.

26. What happens if a class declares no destructor?

a) Objects cannot be destroyed
b) The compiler generates a destructor
c) The class becomes abstract
d) Constructors are deleted
Correct Answer: b) The compiler generates a destructor
Explanation:
An implicit destructor is provided if none is declared.

27. What does a constructor initializer list do?

a) Deletes members
b) Declares friend classes
c) Initializes members before the constructor body runs
d) Calls main
Correct Answer: c) Initializes members before the constructor body runs
Explanation:
Initializer lists perform direct member initialization.

28. Which member usually must be initialized in a constructor initializer list?

a) A mutable member
b) A static member
c) A local variable
d) A reference member
Correct Answer: d) A reference member
Explanation:
References must be bound during initialization.

29. In what order are non-static data members initialized?

a) Declaration order
b) Initializer list order
c) Reverse alphabetical order
d) Constructor body order
Correct Answer: a) Declaration order
Explanation:
Members are initialized in the order they are declared.

30. In derived class construction, what happens first?

a) Derived member initialization
b) Base class construction
c) Destructor call
d) Static member deletion
Correct Answer: b) Base class construction
Explanation:
Base subobjects are constructed before derived parts.

31. In destruction with inheritance, what happens?

a) Base destructor runs first
b) Derived object is never destroyed
c) Derived destructor body runs, then base destruction occurs
d) Only base destructor runs
Correct Answer: c) Derived destructor body runs, then base destruction occurs
Explanation:
Destruction proceeds from most derived to base.

32. What happens if a constructor throws an exception?

a) The object remains fully valid
b) No cleanup occurs
c) The destructor runs for the object
d) Fully constructed subobjects are destroyed
Correct Answer: d) Fully constructed subobjects are destroyed
Explanation:
Construction failure destroys already-constructed parts.

33. What is a delegating constructor?

a) A constructor that calls another constructor in the same class
b) A constructor inherited from base
c) A virtual constructor
d) A static constructor
Correct Answer: a) A constructor that calls another constructor in the same class
Explanation:
Delegating constructors reduce duplicated initialization code.

34. How can base class constructors be inherited in C++11 and later?

a) inherit Base
b) using Base::Base;
c) constructor extends Base
d) import Base
Correct Answer: b) using Base::Base;
Explanation:
A using declaration can inherit base constructors.

35. What does an explicit constructor prevent?

a) Inheritance
b) Overloading
c) Implicit conversions from the constructor argument
d) Destructor execution
Correct Answer: c) Implicit conversions from the constructor argument
Explanation:
explicit requires direct initialization.

36. What is a conversion constructor?

a) A constructor with no parameters
b) A deleted constructor
c) A destructor
d) A constructor that can convert from another type
Correct Answer: d) A constructor that can convert from another type
Explanation:
It allows construction from a compatible argument.

37. What is copy elision?

a) Compiler optimization that omits a copy or move
b) Manual deletion
c) Destructor call
d) Base class initialization
Correct Answer: a) Compiler optimization that omits a copy or move
Explanation:
Copy elision can avoid creating temporary objects.

38. Which functions are part of the Rule of Three?

a) Constructor, destructor, main
b) Destructor, copy constructor, copy assignment operator
c) Move constructor, move assignment, friend
d) Default constructor, static function, destructor
Correct Answer: b) Destructor, copy constructor, copy assignment operator
Explanation:
If one of these is needed, the others often are too.

39. What does the Rule of Five add?

a) Friend and static functions
b) Operator overloading
c) Move constructor and move assignment operator
d) Templates
Correct Answer: c) Move constructor and move assignment operator
Explanation:
C++11 adds move operations to resource management.

40. What is the Rule of Zero?

a) Use no classes
b) Use no constructors
c) Use manual memory only
d) Let default or compiler-generated special members manage resources
Correct Answer: d) Let default or compiler-generated special members manage resources
Explanation:
Prefer classes that do not need custom special member functions.

41. What syntax asks the compiler to generate a default constructor?

a) MyClass() = default;
b) MyClass() = delete;
c) default MyClass();
d) MyClass();
Correct Answer: a) MyClass() = default;
Explanation:
= default requests compiler-generated behavior.

42. What syntax deletes a constructor?

a) = generate
b) = delete
c) = remove
d) = null
Correct Answer: b) = delete
Explanation:
Deleted functions cannot be used.

43. Why would you delete a copy constructor?

a) To speed up loops
b) To make class abstract
c) To prevent copying
d) To allow inheritance
Correct Answer: c) To prevent copying
Explanation:
Deleted copy construction makes objects non-copyable.

44. Can a default constructor be deleted?

a) No
b) Only in templates
c) Only if private
d) Yes
Correct Answer: d) Yes
Explanation:
MyClass() = delete; prevents default construction.

45. Can a copy constructor be defaulted?

a) Yes
b) No
c) Only in C++98
d) Only if virtual
Correct Answer: a) Yes
Explanation:
MyClass(const MyClass&) = default; uses compiler behavior.

46. Can a move constructor be defaulted?

a) No
b) Yes
c) Only if abstract
d) Only if static
Correct Answer: b) Yes
Explanation:
MyClass(MyClass&&) = default; requests default move behavior.

47. If a move constructor is declared, what happens to copy operations?

a) They become virtual
b) They are inherited
c) They are not implicitly available
d) They become protected
Correct Answer: c) They are not implicitly available
Explanation:
Declaring move operations suppresses implicit copy operations.

48. If a destructor is declared, what happens to move operations?

a) They become virtual
b) They are inherited
c) They are always generated
d) They are not implicitly declared
Correct Answer: d) They are not implicitly declared
Explanation:
User-declared destructors suppress implicit move operations.

49. If a copy constructor is declared, what happens to move operations?

a) They are not implicitly declared
b) They become public
c) They are inherited
d) They become static
Correct Answer: a) They are not implicitly declared
Explanation:
Declaring copy operations suppresses implicit move operations.

50. If a copy constructor is declared, what happens to the default constructor?

a) It becomes virtual
b) It is not implicitly declared
c) It becomes protected
d) It becomes deleted automatically
Correct Answer: b) It is not implicitly declared
Explanation:
Any user-declared constructor suppresses the implicit default constructor.

51. What is a user-provided constructor?

a) A defaulted constructor
b) A deleted constructor
c) A constructor defined by the user
d) A compiler-generated constructor
Correct Answer: c) A constructor defined by the user
Explanation:
User-provided constructors have user-written definitions.

52. What does MyClass obj; invoke if available?

a) Copy constructor
b) Move constructor
c) Assignment operator
d) Default constructor
Correct Answer: d) Default constructor
Explanation:
Object creation without arguments uses a default constructor.

53. What does MyClass obj2(obj1); invoke?

a) Copy constructor
b) Assignment operator
c) Destructor
d) Move constructor
Correct Answer: a) Copy constructor
Explanation:
Direct initialization from an object uses copy construction.

54. What does MyClass obj2 = obj1; usually invoke?

a) Destructor
b) Copy constructor
c) Copy assignment operator
d) Move assignment operator
Correct Answer: b) Copy constructor
Explanation:
Copy initialization creates a new object using copy construction.

55. What does obj1 = obj2; invoke when both objects already exist?

a) Copy constructor
b) Move constructor
c) Copy assignment operator
d) Destructor
Correct Answer: c) Copy assignment operator
Explanation:
Assignment to an existing object uses assignment, not construction.

56. What does MyClass obj(std::move(other)); invoke?

a) Copy constructor
b) Destructor
c) Assignment operator
d) Move constructor
Correct Answer: d) Move constructor
Explanation:
An rvalue argument can invoke move construction.

57. Can a constructor initialize a base class?

a) Yes, in the initializer list
b) No
c) Only in destructor
d) Only with friend
Correct Answer: a) Yes, in the initializer list
Explanation:
Derived constructors can pass arguments to base constructors.

58. Where does a derived constructor call a base constructor?

a) In the return statement
b) In the initializer list
c) In the destructor
d) In main
Correct Answer: b) In the initializer list
Explanation:
Base initialization appears after the constructor parameter list.

59. What if a base class has no default constructor?

a) Derived constructors are not needed
b) Derived class cannot have constructors
c) Derived constructor must explicitly call a base constructor
d) Base is ignored
Correct Answer: c) Derived constructor must explicitly call a base constructor
Explanation:
The derived class must choose an accessible base constructor.

60. What if a derived constructor does not explicitly call a base constructor?

a) Compile error always
b) Base is skipped
c) Destructor is called
d) The default base constructor is used if available
Correct Answer: d) The default base constructor is used if available
Explanation:
Otherwise, compilation fails if no default constructor exists.

61. What happens when an array of class objects is created?

a) A constructor is called for each element
b) Only one constructor is called
c) No constructors are called
d) Destructors are called first
Correct Answer: a) A constructor is called for each element
Explanation:
Each array element is constructed.

62. What does new MyClass do?

a) Only allocates memory
b) Allocates memory and calls a constructor
c) Calls destructor
d) Creates a global object
Correct Answer: b) Allocates memory and calls a constructor
Explanation:
new combines allocation and construction.

63. What does delete p do for a pointer to a dynamically allocated object?

a) Only frees memory
b) Calls constructor
c) Calls destructor and frees memory
d) Does nothing
Correct Answer: c) Calls destructor and frees memory
Explanation:
delete destroys the object then releases memory.

64. What does new MyClass[10] do?

a) Calls one constructor only
b) Calls no constructors
c) Calls destructor ten times
d) Calls a constructor for each of the 10 elements
Correct Answer: d) Calls a constructor for each of the 10 elements
Explanation:
Array new constructs every element.

65. What does delete[] arr do for an array of objects?

a) Calls a destructor for each element
b) Calls one destructor only
c) Calls constructor
d) Frees memory without destructors
Correct Answer: a) Calls a destructor for each element
Explanation:
Array delete destroys all elements.

66. Can a destructor be pure virtual?

a) No
b) Yes
c) Only in structs
d) Only if private
Correct Answer: b) Yes
Explanation:
A pure virtual destructor can make a class abstract.

67. What is usually required for a pure virtual destructor?

a) A constructor
b) A static definition
c) A definition outside the class
d) A private destructor
Correct Answer: c) A definition outside the class
Explanation:
Derived destructors need the base destructor definition.

68. When is a virtual destructor especially important?

a) When deleting derived objects through base pointers
b) When using only stack objects
c) When no inheritance is used
d) When constructors are private
Correct Answer: a) When deleting derived objects through base pointers
Explanation:
It ensures the correct destructor chain runs.

69. What can happen if a base destructor is not virtual and a derived object is deleted through a base pointer?

a) Undefined behavior
b) It always works
c) The derived destructor runs
d) It improves performance
Correct Answer: a) Undefined behavior
Explanation:
Only the base destructor may run.

70. Can a destructor be protected?

a) No
b) Yes
c) Only if virtual
d) Only in templates
Correct Answer: b) Yes
Explanation:
Protected destructors restrict who can destroy objects.

71. What does a protected destructor usually do?

a) Makes class abstract
b) Prevents inheritance
c) Restricts destruction to derived classes or friends
d) Deletes copy constructor
Correct Answer: c) Restricts destruction to derived classes or friends
Explanation:
It controls object lifetime access.

72. What does a private destructor usually do?

a) Allows delete anywhere
b) Makes constructor virtual
c) Prevents inheritance
d) Restricts destruction
Correct Answer: d) Restricts destruction
Explanation:
Private destructors prevent normal deletion outside the class.

73. What do smart pointers help with regarding destructors?

a) Automatic destruction
b) Constructor overloading
c) Virtual inheritance
d) Operator overloading
Correct Answer: a) Automatic destruction
Explanation:
Smart pointers delete objects when ownership ends.

74. What does unique_ptr call when it destroys its object?

a) Constructor
b) delete
c) malloc
d) remove
Correct Answer: b) delete
Explanation:
unique_ptr releases its owned object with delete.

75. When does shared_ptr destroy its object?

a) At compile time
b) When main exits only
c) When the last shared_ptr owner is destroyed or reset
d) Never
Correct Answer: c) When the last shared_ptr owner is destroyed or reset
Explanation:
shared_ptr uses reference counting.

76. Does weak_ptr destroy the object it observes?

a) Yes always
b) Only if null
c) Only with delete
d) No
Correct Answer: d) No
Explanation:
weak_ptr does not own the object.

77. Can a constructor have default arguments?

a) Yes
b) No
c) Only destructors
d) Only move constructors
Correct Answer: a) Yes
Explanation:
Default arguments can reduce constructor overloads.

78. Can a constructor be inline?

a) No
b) Yes
c) Only if virtual
d) Only if static
Correct Answer: b) Yes
Explanation:
Constructors defined inside a class are implicitly inline.

79. Can a constructor be constexpr?

a) No
b) Only in C++98
c) Yes, if requirements are met
d) Only for destructors
Correct Answer: c) Yes, if requirements are met
Explanation:
constexpr constructors allow compile-time initialization.

80. What is a common RAII use of a destructor?

a) Define variables
b) Create threads only
c) Call main
d) Release resources acquired by the object
Correct Answer: d) Release resources acquired by the object
Explanation:
RAII ties resource cleanup to object destruction.

81. How can one constructor call another constructor in the same class in C++11?

a) Delegating constructor syntax
b) Virtual inheritance
c) Friend declaration
d) Static call
Correct Answer: a) Delegating constructor syntax
Explanation:
A constructor can delegate to another constructor.

82. Can a constructor be declared in a header and defined in a source file?

a) No
b) Yes
c) Only if inline
d) Only if private
Correct Answer: b) Yes
Explanation:
Constructors can have separate declarations and definitions.

83. Can a destructor be declared in a header and defined in a source file?

a) No
b) Only if virtual
c) Yes
d) Only if default
Correct Answer: c) Yes
Explanation:
Destructors can also be defined out of line.

84. If a class owns a dynamically allocated pointer, what should its destructor usually do?

a) Ignore the pointer
b) Copy it
c) Move it
d) Delete it
Correct Answer: d) Delete it
Explanation:
Destructors release owned dynamic memory.

85. What problem can shallow copying cause when a destructor deletes a pointer?

a) Double deletion
b) Faster execution
c) No effect
d) Better encapsulation
Correct Answer: a) Double deletion
Explanation:
Two objects may delete the same resource.

86. What should a deep-copying copy constructor do?

a) Copy pointer values only
b) Allocate separate resources for the new object
c) Delete the source object
d) Call the destructor
Correct Answer: b) Allocate separate resources for the new object
Explanation:
Deep copies prevent shared ownership problems.

87. What should a move constructor usually do to the source object?

a) Delete it immediately
b) Leave it invalid and dangerous
c) Leave it in a safe state
d) Copy it fully
Correct Answer: c) Leave it in a safe state
Explanation:
Moved-from objects should be safely destructible and assignable.

88. What is a good practice for destructors regarding exceptions?

a) Throw always
b) Throw if possible
c) Catch all and rethrow always
d) Avoid throwing exceptions
Correct Answer: d) Avoid throwing exceptions
Explanation:
Throwing from destructors can cause termination during stack unwinding.

89. What is true about destructors and noexcept?

a) Destructors are noexcept by default in modern C++
b) Destructors cannot be noexcept
c) Destructors always throw
d) Destructors have no exception specification
Correct Answer: a) Destructors are noexcept by default in modern C++
Explanation:
Since C++11, destructors are implicitly noexcept unless specified otherwise.

90. Can a constructor be marked noexcept?

a) No
b) Yes
c) Only if virtual
d) Only if deleted
Correct Answer: b) Yes
Explanation:
noexcept can specify that construction does not throw.

91. If a constructor throws an exception, is the object considered fully constructed?

a) Yes
b) Yes, if destructor exists
c) No
d) Only if base constructed
Correct Answer: c) No
Explanation:
A failed constructor means the object was not created successfully.

92. When are member variables initialized relative to the constructor body?

a) After the body
b) Never
c) During destructor
d) Before the body
Correct Answer: d) Before the body
Explanation:
Member initialization happens before constructor body execution.

93. What is initialized first in a derived class constructor?

a) Base classes
b) Derived member variables
c) Static locals
d) Friend classes
Correct Answer: a) Base classes
Explanation:
Base subobjects are constructed before derived members.

94. When are member variables destroyed?

a) Before the destructor body
b) After the destructor body
c) Before construction
d) Never
Correct Answer: b) After the destructor body
Explanation:
The destructor body runs before members are destroyed.

95. In derived object destruction, when are base class subobjects destroyed?

a) Before destructor body
b) Before members
c) After derived members are destroyed
d) Never
Correct Answer: c) After derived members are destroyed
Explanation:
Destruction order is derived body, members, then bases.

96. When is the destructor of a constructed static local object called?

a) At object creation
b) At constructor exit only
c) Never
d) At program exit
Correct Answer: d) At program exit
Explanation:
Static local objects are destroyed during program termination.

97. When is the destructor of a global object called?

a) At program exit
b) At object creation
c) Only manually
d) Never
Correct Answer: a) At program exit
Explanation:
Global objects are destroyed during program shutdown.

98. What is usually required for an object created with placement new?

a) Calling new again
b) Calling the destructor explicitly
c) Calling delete automatically
d) No cleanup
Correct Answer: b) Calling the destructor explicitly
Explanation:
Placement new separates allocation from construction.

99. What is a factory function useful for?

a) Deleting classes
b) Preventing constructors
c) Controlling object creation
d) Inlining destructors
Correct Answer: c) Controlling object creation
Explanation:
Factories can choose and construct appropriate objects.

100. What is a best practice for managing resources in C++ classes?

a) Manual goto cleanup
b) Global variables only
c) Ignore destructors
d) Use RAII with constructors and destructors
Correct Answer: d) Use RAII with constructors and destructors
Explanation:
RAII provides predictable resource management.
← Previous: C++ Classes and Objects - MCQs
Next →: C++ Control Flow / If-Else MCQs Conditional Statements
NewC++ Classes and Objects - MCQs

C++ Classes and Objects - MCQs

In C++, a class is a fundamental building block of Object-Oriented Programming (OOP) that acts as a user-defined blueprint for…

By MCQs Generator
New
No Image

C++ Inheritance and Polymorphism - MCQs

1. What is the main purpose of a constructor? a) Initialize objects b) Delete objects c) Allocate globals d) Define…

By MCQs Generator
NewC++ Memory Management MCQs

C++ Memory Management MCQs

Memory management in C++ empowers developers with direct control over hardware resources, primarily through the allocation and deallocation of memory…

By MCQs Generator