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 is the output of this program?
#include <iostream>
using namespace std;
class LFC
{
int x;
public:
LFC(int xx, float yy)
{
cout<< char(yy);
}
};
int main()
{
LFC *p = new LFC(35, 99.50f);
return 0;
}
A. 99
B. ASCII value of 99
C. Garbage value
D. 99.5
View Answer
Ans : B
Explanation: float is converted to int and char(int) gives the ASCII value of 99
10. What is the output of this program?
#include <iostream>
using namespace std;
class LFC
{
public:
LFC()
{
cout<< "find";
}
~LFC()
{
cout<< "course";
}
};
int main()
{
LFC obj;
return 0;
}
A. The program will print the output find.
B. The program will print the output course.
C. The program will print the output findcourse.
D. The program will report compile time error.
View Answer
Ans : C
Explanation: As the object created it invokes the constructor and then destructor followed by it.
11. What is the output of this program?
#include <iostream>
using namespace std;
class LFC
{
int x;
public:
LFC();
~LFC();
void Show() const;
};
LFC::LFC()
{
x = 50;
}
void LFC::Show() const
{
cout<< x;
}
int main()
{
LFC obj;
obj.Show();
return 0;
}
A. The program will print the output 50.
B. The program will print the output Garbage-value.
C. The program will report compile time error.
D. The program will report runtime error.
View Answer
Ans : C
Explanation: It's not due to inaccessibility of x. it is very well accessible in find::<> . The destructor is only declared, but not defined anywhere. Hence the compiler throws "" undefined reference to find::~find()"" when it's called after the return statement of main().
12. Which of the following statement is correct about the program given below?
#include <iostream>
using namespace std;
class LFC
{
int *p;
public:
LFC(int xx, char ch)
{
p = new int();
*p = xx + int(ch);
cout<< *p;
}
~LFC()
{
delete p;
}
};
int main()
{
LFC obj(15, 'A');
return 0;
}
A. The program will print the output 80.
B. The program will print the output 112.
C. The program will print the output garbage value.
D. The program will report compile time error.
View Answer
Ans : A
Explanation: Here the character is converted into int i.e. 'A'=65 which is added to the xx value 15 and thus we get the output 80.
13. Which of the following is true about the following program
#include <iostream>
using namespace std;
class LFC
{
int x;
public:
LFC(short ss)
{
cout<< "Short" << endl;
}
LFC(int xx)
{
cout<< "Int" << endl;
}
LFC(float ff)
{
cout<< "Float" << endl;
}
~LFC()
{
cout<< "Final";
}
};
int main()
{
LFC *ptr = new LFC('F');
return 0;
}
A. The program will print the output Short .
B. The program will print the output Final .
C. The program will print the output Float .
D. The program will print the output Int.
View Answer
Ans : D
Explanation: There is no overloaded constructor for char, so compiler will treat 'F' as 70 (ASCII value of F) due to implicit type conversion from char to int.
14. What will be the output of this program?
#include <iostream>
using namespace std;
class LFC
{
private:
int x,y;
public:
void LFC(int a,int b)
{ x=a; y=b;}
};
int main()
{
LFC s;
return 0;
}
A. Compile Time Error
B. Run Time Error
C. No Error
D. Warning
View Answer
Ans : A
Explanation: There is no default constructor in this class definition, and we cannot create an object like this, the correct form of creating object is : find s(10,20); - pass two values because parameterized constructor is exists in the class definition.
15. What will be the output of this program?
#include <iostream>
using namespace std;
class LFC
{
private:
int x;
public:
void LFC(){x=0; printf("Object created.");}
void LFC(int a){x=a;}
};
int main()
{
LFC s;
return 0;
}
A. Compile Time Error
B. Object Created.
C. Run Time Error
D. Cannot be predicted
View Answer
Ans : B
Explanation: Object will be Created.
16. What will be the output of the following program?
#include <iostream>
using namespace std;
int i;
class LFC
{
public:
~LFC()
{
i=10;
}
};
int foo()
{
i=3;
LFC ob;
return i;
}
int main()
{
cout << foo() << endl;
return 0;
}
A. 0
B. 10
C. 3
D. None of the above
View Answer
Ans : B
Explanation: While returning from a function, destructor is the last method to be executed. The destructor for the object ""ob"" is called after the value of i is copied to the return value of the function. So, before destructor could change the value of i to 10, the current value of i gets copied & hence the output is i = 3. See this for more details.
17. Can destuctors be private in C++?
A. Yes
B. No
C. May Be
D. Can't Say
View Answer
Ans : A
Explanation: Destructors can be private. See Private Destructor for examples and uses of private destructors in C++.
18. Allocation of memory to objects at the time of their construction is known as ______ of objects.
A. Run time construction
B. Dynamic Construction
C. Initial Construction
D. Staic Construction
View Answer
Ans : B
Explanation: Allocation of memory to objects at the time of their construction is known as Dynamic Construction of objects
19. We must use initializer list in a constructor when
A. There is a reference variable in class
B. There is a constant variable in class
C. There is an object of another class. And the other class doesn't have default constructor
D. All of the above
View Answer
Ans : D
Explanation: Initializer List is used to initialize data members of a class. The list of members to be initialized is indicated with constructor as a comma separated list followed by a colon.
20. Which of the following implicitly creates a default constructor when the programmer does not explicitly define at least one constructor for a class?
A. Preprocessor
B. Linker
C. Loader
D. compiler
View Answer
Ans : D
Explanation: compiler implicitly creates a default constructor when the programmer does not explicitly define at least one constructor for a class.
21. constructor _______ to allow different approaches of object construction
A. Cannot overloaded
B. Can be overloaded
C. Can be called
D. Can be nested
View Answer
Ans : B
Explanation: constructor Can be overloaded to allow different approaches of object construction
22. When are the Global objects destroyed?
A. When the control comes out of the block in which they are being used
B. When the program terminates
C. When the control comes out of the function in which they are being used.
D. As soon as local objects die
View Answer
Ans : B
Explanation: When the program terminates Global objects destroyed.
23. Whenever const objects try to invoke non-const member functions, the compiler ________
A. Return zero value
B. Return Null
C. Generate error
D. Return no Value
View Answer
Ans : C
Explanation: Whenever const objects try to invoke non-const member functions, the compiler Generate error
24. When a copy constructor may be called?
A. When an object of the class is returned by value
B. When an object of the class is passed (to a function) by value as an argument.
C. Both A & B
D. None of the above
View Answer
Ans : C
Explanation: A copy constructor may be called both a and b statement.
25. 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.
26. 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.
27. If the copy constructor receives its arguments by value, the copy constructor would
A. Call one-argument constructor of the class
B. Work without any problem
C. Call itself recursively
D. Call zero-argument constructor
View Answer
Ans : C
Explanation: If the copy constructor receives its arguments by value, the copy constructor would Call itself recursively.
28. which of this can not be declared as virtual
A. Constructor
B. Destructor
C. Both A & B
D. None of the above
View Answer
Ans : C
Explanation: Constructor and Destructor this can not be declared as virtual.
29. For automatic objects, constructors and destructors are called each time the objects
A. Enter and leave scope
B. Inherit parent class
C. Are constructed
D. Are destroyed
View Answer
Ans : A
Explanation: For automatic objects, constructors and destructors are called each time the objects Enter and leave scope.
30. An _________ with a constructor or destructor cannot be used as a member or a union
A. Class
B. Object
C. Function.
D. Varible
View Answer
Ans : B
Explanation: An object with a constructor or destructor cannot be used as a member or a union.
31. Choose the correct statements.
A. A destructor is not inherited
B. A constructor cannot be called explicitly
C. A constructor is not inherited
D. All of the above
View Answer
Ans : D
Explanation: All of the above are the correct statements.
32. Which of the following remarks about the differences between constructors and destructors are correct ?
A. Constructors can take arguments but destructors cannot.
B. Constructors can be overloaded but destructors cannot be overloaded.
C. Destructors can take arguments but constructors cannot.
D. Both A and B
View Answer
Ans : D
Explanation: Both A and B are correct
Also check :
Discussion