Machine-translated by Google. Questions are authored in English.
Top

Constructors and Destructors Questions and Answers

1.

The copy constructor must receive its arguments by :

Exam Year(s)

MH SET - 2018, SET Exam - 2018,

Answer: A

The copy constructor must receive its argument only by reference.

A copy constructor has the form X(const X& other). If the argument were passed by value, calling the copy constructor would require making a copy of the argument, which would call the copy constructor again, recursing without end. C++ therefore forbids a copy constructor that takes its own class by value.

Passing by address (a pointer) gives an ordinary constructor, not a copy constructor, so 'only by address', 'only by value' and 'either by value or by reference' are all wrong.

Please log in or register to report this question.

2.

Which one of the following ensures proper cleanup of objects of the type ?

(i) Destructor
(ii) Delete
(iii) Exception Handling
(iv) Garbage Collection

Exam Year(s)

MH SET - 2019, SET Exam - 2019,

Answer: A

The answer is (i) only, the destructor.

In C++, the destructor is the member function that the language calls automatically when an object goes out of scope or is deleted. It releases the object's resources, such as memory, files and locks, so it is what ensures proper cleanup for objects of the type.

delete is an operator that ends a dynamically allocated object's life by calling its destructor, so it relies on the destructor rather than defining the cleanup. Exception handling deals with error conditions, and garbage collection reclaims memory in some languages but runs at unpredictable times and does not provide deterministic cleanup.

Please log in or register to report this question.

Loading…

Other Subjects

Tags: Constructors and Destructors Questions and Answers || Constructors and Destructors MCQ Questions and Answers || Constructors and Destructors GK Questions and Answers || Constructors and Destructors GK MCQ Questions || Constructors and Destructors Multiple Choice Questions and Answers || Constructors and Destructors GK || GK on Constructors and Destructors || Object Oriented Programming Questions and Answers || Object Oriented Programming MCQ Questions and Answers || Object Oriented Programming GK Questions and Answers || GK on Object Oriented Programming