Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

What will be the output of the following C++ code?
#include <iostream>
using namespace std;
class A{
public:
A(){
cout<<“Constructor called\n”;
}
~A(){
cout<<“Destructor called\n”;
}
};
int main(int argc, char const *argv[])
{
A *a = new A[5];
delete[] a;
return 0;
}

What will be the output of the following C++ code?
#include <iostream>
using namespace std;
class A{
public:
A(){
cout<<“Constructor called\n”;
}
~A(){
cout<<“Destructor called\n”;
}
};
int main(int argc, char const *argv[])
{
A *a = new A[5];
delete[] a;
return 0;
}
a) “Constructor called” five times and then “Destructor called” five times
b) “Constructor called” five times and then “Destructor called” once
c) Error
d) Segmentation fault

Answer: a
Explanation: In this, we have first initiated five-pointer variables using new keyword hence fives time constructor will be called after that as we using delete[](used for deleting multiple objects) to delete variables hence all the five objects created will be destroyed and hence five times destructor will be called.

Join The Discussion