Quiz on Constructor & Destructor in C++
Constructors and Destructors in C++ are special member functions of a class. Each object is initialized by a constructor. As soon as the compiler creates the object, a call is made to the constructor. Object members are initialized by constructors after storage for the object is allocated. Meanwhile, a class object is destroyed using the Destructor. Take this quick quiz to test your knowledge about these special C++ functions. Happy Learning.
0 of 15 Questions completed Questions: You have already completed the quiz before. Hence you can not start it again. Quiz is loading… You must sign in or sign up to start the quiz. You must first complete the following: 0 of 15 Questions answered correctly Your time: Time has elapsed You have reached 0 of 0 point(s), (0) Earned Point(s): 0 of 0, (0) What is the output of the following code snippet? What is the output of the following code snippet? What is the output of the following code snippet? When a control reaches the end of its class scope, which of the following is automatically executed? What is the method for initializing an object of a class and allocating memory space? What is the output of the following code snippet? What is the return type of the constructor? What is the output of the following code snippet? What is the output of the following code snippet? What is the output of the following code snippet? #include <iostream> class Cons { cout << endl; What is the output of the following code snippet? What is the output of the following code snippet? What is the output of the following code snippet? What is the output of the following code snippet? What is the output of the following code snippet? cout << “Constructor is called for data:” Quiz Summary
Information
Results
Results
0 Essay(s) Pending (Possible Point(s): 0) Categories
1. Question
#include <iostream>
using namespace std;
class Const
{
public:
int a, b;
Const()
{
a = 0;
b = 0;
}
};
int main()
{
Const c;
cout << “a: ” << c.a << endl<< “b: ” << c.b;
return 0;
} 2. Question
#include <iostream>
using namespace std;
class Const
{
public:
int a;
public:
Const()
{
a = 0;
}
Const(int i)
{
a=i;
cout<<“a:”<<a;
}
void display()
{
cout<<“a:”<<a;
}
};
int main()
{
Const c(10);
c.display();
c=30;
return 0;
} 3. Question
#include <iostream>
using namespace std;
class Const
{
public:
int a;
public:
Const()
{
}
Const(Const &i)
{
}
};
Const con()
{
cout<<“con() function called”;
Const c;
return c;
}
int main()
{
Const c;
c=con();
return 0;
} 4. Question
5. Question
6. Question
#include <iostream>
using namespace std;
class Const
{
public:
int a;
public:
Const()
{
}
Const(Const &i)
{
}
};
Const con()
{
cout<<“con() function called”;
Const c;
return c;
}
int main()
{
Const c;
Const c1=con();
return 0;
} 7. Question
8. Question
#include <iostream>
using namespace std;
class Con{
const char* q;
public:
Con()
{
q = new char[10];
q = “QuizOrbit”;
}
void display()
{
cout << q << endl;
}
};
int main()
{
Con c;
c.display();
} 9. Question
#include <iostream>
using namespace std;
class Con{
const char* q;
public:
Con()
{
q = new char[10];
q = “QuizOrbit”;
}
void display()
{
cout << q << endl;
}
};
int main()
{
Con c=new Con();
c.display();
} 10. Question
using namespace std;
int* q;
public:
Cons()
{
q = new int[3]{ 1, 2, 3 };
for (int i = 0; i < 3; i++) {
cout << q[i] << ” “;
}
}
};
int main()
{
Cons* ptr = new Cons[2];
} 11. Question
#include <iostream>
using namespace std;
int d;
class Des
{
public:
~Des()
{
cout<<“Destructor called”<<” “;
}
};
int fun()
{
d=5;
Des q;
return d;
}
int main()
{
cout << fun() << endl;
return 0;
} 12. Question
#include <iostream>
using namespace std;
class Des
{
int i;
static int count;
public:
Des() {
count++;
i = count;
cout << “constructor called for id: ” << i << endl;
}
~Des() {
cout << “Destructor called for id:” << i<< endl;
}
};
int Des::count = 0;
int main() {
Des d[2];
return 0;
} 13. Question
#include <iostream>
using namespace std;
class Parent {
public:
Parent()
{
cout << “Constructor called”<<endl;
}
~Parent()
{
cout << “Destructor called”<<endl;
}
};
class Child : public Parent {
public:
Child()
{
cout << “Constructor called \n”;
}
~Child()
{
cout << “Destructor called \n”;
}
};
int main(void)
{
Child* c = new Child();
Parent* p = c;
delete p;
return 0;
} 14. Question
// CPP program without virtual destructor
// causing undefined behavior
#include <iostream>
using namespace std;
class Parent {
public:
Parent(int a)
{
cout << “Constructor called”<<a<<endl;
}
~Parent()
{
cout << “Destructor called”<<endl;
}
};
class Child : public Parent {
public:
Child()
{
cout << “Constructor called \n”;
}
~Child()
{
cout << “Destructor called \n”;
}
};
int main(void)
{
Child* c = new Child();
Parent* p = c;
delete p;
return 0;
} 15. Question
// CPP program without virtual destructor
// causing undefined behavior
#include <iostream>
#include <vector>
using namespace std;
class Parent {
int* d;
public:
Parent(int a)
{
d = new int;
*d = a;
<< a << endl;
}
Parent(const Parent& s)
: Parent{ *s.d }
{
cout << “Copy Constructor is called for data d:”<<*d<< “and – “<< “copy for s: “<< *s.d<< endl;
}
~Parent()
{
if (d != nullptr)
cout << “Destructor is called for data d:”<< *d<< endl;
else
cout << “Destructor is called”
<< ” for nullptr”
<< endl;
delete d;
}
};
int main(void)
{
vector<Parent> v;
v.push_back(Parent{ 30 });
v.push_back(Parent{ 50 });
return 0;
}
Summary
Programmers use special functions as one of their most important tools. You may have learned something about Constructors and Destructors by taking the quiz. Before moving forward, every programmer needs to understand this topic. Have a Good Learning Experience!
Shashwat Sharma