Difficulty: Easy
Correct Answer: Constructor is called either implicitly or explicitly, whereas destructor is always called implicitly.
Explanation:
Introduction / Context:
C++ manages object lifetimes using constructors (initialization) and destructors (cleanup). Most of the time these functions are invoked by the language at well-defined points. This item tests awareness of typical invocation patterns.
Given Data / Assumptions:
Concept / Approach:
Constructors are often called implicitly (e.g., T x; or new T()). In advanced scenarios (placement new), a constructor can be invoked explicitly. Destructors are typically invoked implicitly at scope exit or via delete. Although C++ permits explicitly calling a destructor, idiomatic and safe resource management relies on the implicit calls, which is the intent behind the conventional MCQ answer.
Step-by-Step Solution:
1) Automatic T x; → constructor runs implicitly; at scope end, destructor runs implicitly.2) Dynamic T* p = new T; → constructor via new; delete p; → destructor runs implicitly.3) Placement new: new (buffer) T(args); → explicit constructor syntax exists.4) General rule captured by the option: constructors may be implicit or explicit; destructors (in normal use) are implicitly called.
Verification / Alternative check:
Instrument constructors/destructors with prints; observe automatic, timely calls without manual invocation in ordinary patterns.
Why Other Options Are Wrong:
Always explicit/always not called: contradicts standard object lifetime semantics.Destructor always explicit: incorrect in idiomatic C++ (delete/scope end trigger implicit calls).
Common Pitfalls:
Manually calling a destructor on an object with automatic storage, then letting scope end invoke it again, causes double-destruction. Avoid explicit calls unless you precisely control object lifetimes (e.g., placement new patterns).
Final Answer:
Constructor is called either implicitly or explicitly, whereas destructor is always called implicitly.
Discussion & Comments