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: d
Explanation: The program will result in segmentation fault as we are trying to delete only one pointer variable and leaving other variables as it is which will result in segmentation fault i.e. improper handling of memory.

Join The Discussion