Exercise: Questions on Destructors


Questions on Destructors : Question 1 :
Can destuctors be private in C++?

Yes
No
Show Answer
Questions on Destructors : Question 2 :
Predict 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;
}

0
3
10
None of the above
Show Answer
Questions on Destructors : Question 3 :
Like constructors, can there be more than one destructors in a class?

Yes
No
Show Answer
Questions on Destructors : 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 1
constructor for id 1
constructor for id 2
constructor for id 3
destructor for id 1
destructor for id 2
destructor for id 3
Compiler Dependent.
constructor for id 1
destructor for id 1
Show Answer
Questions on Destructors : Question 5 :
Can destructors be virtual in C++?

Yes
No
Show Answer