Constructors&destructor

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Constructor & Destructor

Fill in the Blanks


1. __________ can be used to initialize the data members at the time of
object creations.

2. Name of the constructor of class main will be _________

3. _______ is the first member function to be executed when an object


of that class is created.

4. Copy constructor takes a _____ to an object of the same class as an


argumnets.

5. Overloaded constructors differ in their_______

6. Name of the Destructor is preceeded by the _____ symbol.

7. The constructor of the global objects gets called_________

8. constant object can be initialized only ____________

State True or False

1. A constructor is automatically invoked when an object goes of scope


2. A constructor can be declared only in the public section.
3. A constructor can be called explicitly
4. A function can be oveloaded
5. Constructor and destructor must be defined inside the class.
6. Copy constructor allocates memory for the data members
dynamically.
7. Constructor can also have default arguments
8. it is compulsory to place defualt arguments in the defination of a
constructor.
9. It is possible to overload constructors.
10. destructor is called every time an object is created.
11. The order of invoking a destructor is the same as that of invoking a
constructor.
12. The address of constructor and destructors can be accessed in
programs.
13. An object with a constructor or destructor cannot be used as a
member of a union.
14. constructor and destructor can be inherited.
15. Constructor can be virtual, but destructor can not be virtual.
16. Anonymous classes can also have constructors and destructos.
17. Destructors can be overloaded.
18. Constructos and Destructors can return values

Multiple Choice Questions


1. A constructor can be
a) virtual b) Static c)Voilatile d). none of these

2. Which constructor does not initialize any data members


a) Dummy b)Default c)Copy d) parameterised.

3. Which constructor does not take any arguments?


a) Dummy b)Default c)Copy d) parameterised.

4. Which constructor creates a new object from an existing one?


a) Dummy b)Default c)Copy d) parameterised.

5. Which types of constructors is similar to a constructor that has all


default arguments?
a) Dummy b)Default c)Copy d) parameterised.

6. If you write student s[30]; where student is the name of the class ,
how many times will the constructor function be invoked
a) 29 b) 1 c) 30 d) 31

7. Ideally , in which section must constructor and destructor be


declared.
a) private b) public c) protected d)any of theese
8. How many destructors can have a class
a) 0 b) 1 c) 2 d) N

9. Constant object can be initialized only by_______


a) Constructor b)member function c) Main() d) destructor

10. How many Anonymous objects can a class have at a particular time?
a) 0 b) 1 c) 2 d) N
11. Constructor and destructor are automatically invoked by
a) operating system b) compiler c) Main() d) object.

12. Sample s1 = s2; will invoke ____ types of constructor?


a) Dummy b)Default c)Copy d) parameterised.

Analyse the following Codes


1. #include<iostream>
using namespace std;
class A
{
A() {
cout<<”Constructor”<<endl;
}
~A() {
cout<<”Destructor”<<endl;
}
};
main(){
A obj1;
}

2.#include<iostream>
using namespace std;
class A
{
int x;
public:
A(int a) {
x=a;
cout<<”Constructor”<<endl;
}
~A() {
cout<<”Destructor”<<endl;
}
};
main() {
A obj1;
}

3.#include<iostream>
using namespace std;
class A
{
int x;
public:
A(a) {
x=a;
cout<<”Constructor”<<endl;
}
~A() {
cout<<”Destructor”<<endl;
}
};
main() {
A obj1;
}

4.#include<iostream>
using namespace std;
class A
{
int x;
public:
A(int a) {
x=a;
cout<<”Constructor”<<endl;
}
void get_data() {
cout<<”x=”<<x<<endl;
}
};
main() {
A obj1;
obj1.get_data();
}

5.#include<iostream>
using namespace std;
class abcd
{
~abcd() {
cout << "welcome to vector india";
}
public:
abcd() {
cout << "welcome to bangalore";
}
};
int main()
{
abcd vector;
}

6.#include<iostream>
using namespace std;
class Point
{
Point() {
cout << "Constructor called";
}
};
int main() {
Point t1;
}
7.#include<iostream>
using namespace std;
class Ex
{
public:
void ~Ex() {
cout<<"Destroying the object";
}
};
int main() {
Ex abcd;
}

8.#include<iostream>
using namespace std;
class Exam
{
public:
Exam() {
cout << "Constructor called ";
}
};
int main()
{
Exam Ex1, Ex2;
}
9.#include<iostream>
using namespace std;
class ample
{
public:
int a;
int b;
};
int main()
{
ample Ex1 = {10, 20};
cout << "a = " << Ex1.a <<", b = " << Ex1.b;
}

You might also like