• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests

C++ Destructors

Question 1

Can destructors be private in C++?
  • Yes
  • No

Question 2

Predict the output of following C++ progran CPP
#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

Question 3

Like constructors, can there be more than one destructors in a class?
  • Yes
  • No

Question 4

CPP
#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
    

Question 5

Can destructors be virtual in C++?
  • Yes
  • No

There are 5 questions to complete.

Last Updated :
Take a part in the ongoing discussion