1. Home
  2. Computer Science MCQs
  3. C++ and Object-Oriented Programming
  4. When is a C++ destructor automatically invoked?

Added 30 Nov -0001

When is a C++ destructor automatically invoked?

  1. Immediately after the constructor finishes
  2. Whenever the object is passed to a function
  3. When the object lifetime ends, such as scope exit Correct answer
  4. Only when the programmer calls delete explicitly on a pointer

Correct answer

C. When the object lifetime ends, such as scope exit

15 views

MCQSEXAM

Explanation

A destructor, written ~ClassName(), runs automatically when the object goes out of scope, is explicitly deleted, or is destroyed as a member of a dying container. Its job is to release resources the constructor acquired, from heap memory to file handles. For objects with automatic storage the compiler inserts the call, so a destructor should never be invoked manually on such an object.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is C? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More C++ and Object-Oriented Programming MCQs

Practise all C++ and Object-Oriented Programming MCQs

Similar MCQs