C++ Programming Questions and Answers – Constructors and Destructors – 1

«
»

This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “Constructors and Destructors – 1”.

1. What is the role of a constructor in classes?
a) To modify the data whenever required
b) To destroy an object
c) To initialize the data members of an object when it is created
d) To call private functions from the outer world
View Answer

Answer: c
Explanation: A constructor is used in classes to initialize data members of class in order to avoid errors/segmentation faults.
advertisement

2. Why constructors are efficient instead of a function init() defined by the user to initialize the data members of an object?
a) Because user may forget to call init() using that object leading segmentation fault
b) Because user may call init() more than once which leads to overwriting values
c) Because user may forget to define init() function
d) All of the mentioned
View Answer

Answer: d
Explanation: We cannot use init() because as mentioned in options that user may forget to initialize the members which will lead to a segmentation fault. Also if the user calls the init() function more than once it may overwrite the values and may result into disastrous results. Also if any user forgets to define init() function then no object will be initialized whereas if any constructor is not defined in any class the class provides a default constructor for initialization.

3. What is a copy constructor?
a) A constructor that allows a user to move data from one object to another
b) A constructor to initialize an object with the values of another object
c) A constructor to check the whether to objects are equal or not
d) A constructor to kill other copies of a given object.
View Answer

Answer: b
Explanation: Copy constructor allows the user to initialize an object with the values of another object instead of supplying the same set of values again to initialize the object.

4. What will be the output of the following C++ code?

#include <iostream>
#include <string>
using namespace std;
class A{
	int a;
public:
	A(int i){
		a = i;
	}
	void assign(int i){
		a = i;
	}
	int return_value(){
		return a;
	}
};
int main(int argc, char const *argv[])
{
	A obj;
	obj.assign(5);
	cout<<obj.return_value();
}

a) 5
b) 55
c) Error
d) Segmentation Fault
View Answer

Answer: c
Explanation: As we have defined a constructor which takes an int parameter, so when we are trying to declare an object obj of class A without supplying any parameter then as a constructor is overwritten it will give an error saying that no matching function found. So whenever one writes a constructor then the default constructor is overwritten hence if you want to declare an object without parameter then you also have to define that constructor.

5. What will be the output of the following C++ code?

advertisement
#include <iostream>
#include <string>
using namespace std;
class A{
	int a;
	A(){
		a = 5;
	}
 
public:
	void assign(int i){
		a = i;
	}
	int return_value(){
		return a;
	}
};
int main(int argc, char const *argv[])
{
	A obj;
	obj.assign(10);
	cout<<obj.return_value();
}

a) 5
b) 10
c) Error
d) Segmentation fault
View Answer

Answer: c
Explanation: Here the constructor is made private and as no object can access any private object directly therefore the program will give error. One should always define a constructor as public.

6. In the following C++ code how many times the string “A’s constructor called” will be printed?

#include <iostream>
#include <string>
using namespace std;
class A{
	int a;
public:
	A(){
		cout<<"A's constructor called";
	}
};
class B{
	static A a;
public:
	B(){
		cout<<"B's constructor called";
	}
	static A get(){
		return a;
	}
};
A B::a;
int main(int argc, char const *argv[])
{
	B b;
	A a1 = b.get();
	A a2 = b.get();
	A a3 = b.get();
}

a) 3
b) 4
c) 2
d) 1
View Answer

Answer: d
Explanation: As the object is defined ony once in the program at line A B::a, so the constructor of A is called only once. For objects a1, a2 and a3 copy constructor is called so the string will not be printed for them.

7. What happens if a user forgets to define a constructor inside a class?
a) Error occurs
b) Segmentation fault
c) Objects are not created properly
d) Compiler provides a default constructor to avoid faults/errors
View Answer

Answer: d
Explanation: The C++ compiler always provides a default constructor if one forgets to define a constructor inside a class.

8. How many parameters does a default constructor require?
a) 1
b) 2
c) 0
d) 3
View Answer

Answer: c
Explanation: A default constructor does not require any parameters for object creation that’s why sometimes we declare an object without any parameters.
advertisement

9. How constructors are different from other member functions of the class?
a) Constructor has the same name as the class itself
b) Constructors do not return anything
c) Constructors are automatically called when an object is created
d) All of the mentioned
View Answer

Answer: d
Explanation: All the above mention are the reasons where constructor differs from other normal member functions of a class.

10. How many types of constructors are there in C++?
a) 1
b) 2
c) 3
d) 4
View Answer

Answer: c
Explanation: There are three types of constructors in C++ namely default, parameterized and copy constructor.

11. What will be the output of the following C++ code?

#include <iostream>
#include <string>
using namespace std;
class A{
	mutable int a;
public:
	A(){
		cout<<"Default constructor called\n";
	}
	A(const A& a){
		cout<<"Copy Constructor called\n";
	}
};
int main(int argc, char const *argv[])
{
	A obj;
	A a1 = obj;
	A a2(obj);
}

a)

Default constructor called
Copy Constructor called

b)

Default constructor called
Copy Constructor called
Copy Constructor called

c)

Default constructor called
Default constructor called
Copy Constructor called

d)

Copy Constructor called
Default constructor called
Copy Constructor called
View Answer
Answer: b
Explanation: When object obj is declared then the default constructor is called. When we are declaring the a1 object as we are equating obj to a1 object obj will be copied to a1 hence copy constructor is called, similarly when object a2 is created obj is passed as a parameter to function which is available as copy constructor function, hence copy constructor will be called. So one time Default constructor and two times copy constructor.
 
 

advertisement

12. What will be the output of the following C++ code?

#include <iostream>
#include <string>
using namespace std;
class A{
	mutable int a;
public:
	A(){
		cout<<"A's default constructor called\n";
	}
	A(const A& a){
		cout<<"A's copy Constructor called\n";
	}
};
class B{
	A obj;
public:
	B(){
		cout<<"B's Constructor called\n";
	}
};
int main(int argc, char const *argv[])
{
	B b1;
	B b2;
}

a)

B's Constructor called
B's Constructor called

b)

B's Constructor called
A's default constructor called
B's Constructor called
A's default constructor called

c)

A's default constructor called
B's Constructor called
A's default constructor called
B's Constructor called

d)

A's default constructor called
B's Constructor called
A's copy Constructor called
View Answer
Answer: c
Explanation: Here when we are declaring the object b1 of class B then first the constructor of class B will be called, in which first it will initialize all the members of class B and as obj from class A is member of class B and it should be initialized so the A’s default constructor will be called and terminates after that B’s constructor terminates hence A’s default constructor called is printed before B’s constructor called.
 
 

13. What is the role of destructors in Classes?
a) To modify the data whenever required
b) To destroy an object when the lifetime of an object ends
c) To initialize the data members of an object when it is created
d) To call private functions from the outer world
View Answer

Answer: b
Explanation: Destructors are used in Classes to destroy an object after its lifetime is over i.e. to free resources occupied by that object.

14. What is syntax of defining a destructor of class A?
a) A(){}
b) ~A(){}
c) A::A(){}
d) ~A(){};
View Answer

Answer: b
Explanation: A destructor starts with a ~(tilde) symbol, has the same name as the class.

15. When destructors are called?
a) When a program ends
b) When a function ends
c) When a delete operator is used
d) All of the mentioned
View Answer

Answer: d
Explanation: Destructors are called at the following time:
i) at the end of the program to destroy objects declared in the main() or global scope.
ii) at the end of a function to destroy objects declared at that function scope.
iii) when user by himself tries to delete an object using the delete operator.
iv) at the end of a block to destroy objects declared at that block scope.

Sanfoundry Global Education & Learning Series – C++ Programming Language.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn