C++ Programming Multiple Choice Questions - Constructor And Destructor
This section focuses on the "Constructor And Destructor" in C++ programming langauge. These Multiple Choice Questions (MCQ) should be practiced to improve the C++ programming skills required for various interviews (campus interview, walk-in interview, company interview), placement, entrance exam and other competitive examinations.
1. Which of the followings is/are automatically added to every class, if we do not write our own.
A. Copy Constructor.
B. Assignment Operator
C. A constructor without any parameter
D. All of the above
View Answer
Ans : D
Explanation: In C++, if we do not write our own, then compiler automatically creates a default constructor, a copy constructor and a assignment operator for every class..
2. Which of the following gets called when an object is being created?
A. Constuctor
B. Virtual Function
C. Destructors
D. Main
View Answer
Ans : A
Explanation: Virtual Function gets called when an object is being created.
3. Destructor has a same name as the constructor and it is preceded by?
A. !
B. ?
C. ~
D. $
View Answer
Ans : C
Explanation: Destructor has a same name as the constructor and it is preceded by ~.
4. Like constructors, can there be more than one destructors in a class?
A. Yes
B. No
C. May Be
D. Can't Say
View Answer
Ans : B
Explanation: There can be only one destructor in a class. Destructor's signature is always ~ClassNam()and they can not be passed arguments.
5. State whether the following statements about the constructor are True or False.
i) constructors should be declared in the private section.
ii) constructors are invoked automatically when the objects are created.
A. True,True
B. True,False
C. False,True
D. False,False
View Answer
Ans : C
Explanation: Statement 1 is false and statement 2 is true.
6. Which of the following is true about constructors.
i) They cannot be virtual
ii) They cannot be private.
iii) They are automatically called by new operator.
A. All i,ii,iii
B. i & iii
C. ii & iii
D. i & ii
View Answer
Ans : B
Explanation: i) True: Virtual constructors don't make sense, it is meaningless to the C++ compiler to create an object polymorphically. ii) False: Constructors can be private
7. Destructors __________ for automatic objects if the program terminates with a call to function exit or function abort
A. Are called
B. Are not called
C. Are inherited
D. Are created
View Answer
Ans : B
Explanation: Destructors Are not called for automatic objects if the program terminates with a call to function exit or function abort
8. Which contructor function is designed to copy object of same class type?
A. Copy constructor
B. Create constructor
C. Object constructor
D. Dynamic constructor
View Answer
Ans : A
Explanation: Copy constructor function is designed to copy object of same class type.
9. What will be the output of the following program?
#include <iostream>
using namespace std;
class LFC
{
int id;
static int count;
public:
LFC() {
count++;
id = count;
cout << "constructor for id " << id << endl;
}
~LFC() {
cout << "destructor for id " << id << endl;
}
};
int LFC::count = 0;
int main() {
LFC a[3];
return 0;
}
A. constructor for id 1 constructor for id 2 constructor for id 3 destructor for id 3 destructor for id 2 destructor for id 1
B. constructor for id 1 constructor for id 2 constructor for id 3 destructor for id 1 destructor for id 2 destructor for id 3
C. Compiler Dependent
D. constructor for id 1
destructor for id 1
View Answer
Ans : D
Explanation: In the above program, id is a static variable and it is incremented with every object creation. Object a[0] is created first, but the object a[2] is destroyed first. Objects are always destroyed in reverse order of their creation. The reason for reverse order is, an object created later may use the previously created object.
10. What will be the output of the following program?
#include <iostream>
using namespace std;
class LFC {
LFC() { cout << "Constructor called"; }
};
int main()
{
LFC t1;
return 0;
}
A. Compiler Error
B. Runtime Error
C. Constructor called
D. destructor for id 1
View Answer
Ans : A
Explanation: By default all members of a class are private. Since no access specifier is there for find(), it becomes private and it is called outside the class when t1 is constructed in main.
Also check :
Discussion