My Report

C++ Constructor and Destructor Test – 1


Correct Answer: 2 points | Wrong: -1 point
Grades: A* (100% score) | A (80%-99%) | B (60%-80%) | C (40%-60%) | D (0%-40%)
advertisement

1. What is the role of a constructor in classes?

2. How constructors are different from other member functions of the class?

3. What happens if a user forgets to define a constructor inside a class?

4. 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);
}

5. 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();
}

6. When destructors are called?

7. How many parameters does a default constructor require?

8. What is syntax of defining a destructor of class A?

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

#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();
}

10. What is the role of destructors in Classes?

11. How many types of constructors are there in C++?

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;
}

13. Why constructors are efficient instead of a function init() defined by the user to initialize the data members of an object?

14. What is a copy constructor?

15. 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();
}

 

Start practicing “1000 MCQs on C++”, and once you are ready, you can take tests on all topics by attempting our “C++ Test Series”.

Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.