C++ Destructors C++ Destructors Please wait while the activity loads. If this activity does not load, try refreshing your browser. Also, this page requires javascript. Please visit using a browser with javascript enabled. If loading fails, click here to try again Question 1Can destructors be private in C++?YesNoC++ Destructors Discuss itQuestion 1-Explanation: Destructors can be private. See Private Destructor for examples and uses of private destructors in C++.Question 2Predict the output of following C++ progran #include <iostream> using namespace std; int i; class A { public: ~A() { i=10; } }; int foo() { i=3; A ob; return i; } int main() { cout << foo() << endl; return 0; } 0310None of the aboveC++ Destructors Discuss itQuestion 2-Explanation: While returning from a function, destructor is the last method to be executed. The destructor for the object “ob” is called after the value of i is copied to the return value of the function. So, before destructor could change the value of i to 10, the current value of i gets copied & hence the output is i = 3. See this for more details.Question 3Like constructors, can there be more than one destructors in a class?YesNoC++ Destructors Discuss itQuestion 3-Explanation: There can be only one destructor in a class. Destructor\'s signature is always ~ClassNam() and they can not be passed arguments. Question 4 #include <iostream> using namespace std; class A { int id; static int count; public: A() { count++; id = count; cout << "constructor for id " << id << endl; } ~A() { cout << "destructor for id " << id << endl; } }; int A::count = 0; int main() { A a[3]; return 0; } constructor for id 1 constructor for id 2 constructor for id 3 destructor for id 3 destructor for id 2 destructor for id 1constructor for id 1 constructor for id 2 constructor for id 3 destructor for id 1 destructor for id 2 destructor for id 3Compiler Dependent.constructor for id 1 destructor for id 1C++ Destructors Discuss itQuestion 4-Explanation: In the above program, id is a static variable and it is incremented with every object creation. Object a[0] is created first, but the object a[2] is destroyed first. Objects are always destroyed in reverse order of their creation. The reason for reverse order is, an object created later may use the previously created object. For example, consider the following code snippet. A a; B b(a); In the above code, the object ‘b’ (which is created after ‘a’), may use some members of ‘a’ internally. So destruction of ‘a’ before ‘b’ may create problems. Therefore, object ‘b’ must be destroyed before ‘a’.Question 5Can destructors be virtual in C++?YesNoC++ Destructors Discuss itQuestion 5-Explanation: See http://www.geeksforgeeks.org/g-fact-37/ 1 There are 5 questions to complete. You have completed questions question Your accuracy is Correct Wrong Partial-Credit You have not finished your quiz. If you leave this page, your progress will be lost. Correct Answer You Selected Not Attempted Final Score on Quiz Attempted Questions Correct Attempted Questions Wrong Questions Not Attempted Total Questions on Quiz Question Details Results Date Score Hint Time allowed minutes seconds Time used Answer Choice(s) Selected Question Text Need more practice! Keep trying! Not bad! Good work! Perfect! Last Updated : 28 Sep, 2023 Share your thoughts in the comments Add Your Comment Please Login to comment...