Vidyalelo
C++ Programming · all questions

Constructors and Destructors in C++
practice.

Practice every MCQ with options. Use Show answers when you want the correct option and solution.

77

Questions

4/4

Page

Pick an option on a question to see the right answer and solution.

What will be the output of the following C++ code?
#include <iostream>  
using namespace std;
class A{
	A(){
		cout<<"A's Constructor called\n";
	}
};
class B
{
public:
	A a;
	B(){
		cout<<"B's constructor called\ns";
	}
};
int main(int argc, char const *argv[])
{
	B b;
	return 0;
}

Select an option to see the answer and solution.

What is a copy constructor?

Select an option to see the answer and solution.

When a copy constructor is called?

Select an option to see the answer and solution.

Which of the following represents the correct explicit call to a constructor of class A?
class A{
		int a;
	        public:
		A(int i)
                {
			a = i;
		}
       }

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <iostream>  
using namespace std;
class A{
	~A(){}
};
class B
{
public:
	A a;
};
int main(int argc, char const *argv[])
{
	B b;
	return 0;
}

Select an option to see the answer and solution.

How many parameters does a default constructor require?

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.

What is the role of destructors in Classes?

Select an option to see the answer and solution.

What is syntax of defining a destructor of class A?

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <iostream>  
using namespace std;
class A{
public:
	int a;
};
int main(int argc, char const *argv[])
{
	A a1 = {10};
	A a2 = a1;
	cout<<a1.a<<a2.a;
	return 0;
}

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <iostream>  
using namespace std;
class A{
public:
	int a;
	A(){
		cout<<"Constructor called\n";
	}
} a;
int main(int argc, char const *argv[])
{
	cout<<"Main function\n";
	return 0;
}

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.

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 Constructor called\n";
	}
	~A(){
		cout<<"A's Destructor called\n";
	}
};
class B: public A{
	A a;
public:
	B(){
		cout<<"B's Constructor called\n";
	}
	~B(){
		cout<<"B's Destructor called\n";
	}
};
int main(int argc, char const *argv[])
{
	B b1;
}

Select an option to see the answer and solution.

What will be the output of the following C++ code?
#include <iostream>  
using namespace std;
class A{
	A(){
		cout<<"A's Constructor called\n";
	}
	friend class B;
};
class B
{
public:
	A a;
	B(){
		cout<"B's constructor called\ns";
	}
};
int main(int argc, char const *argv[])
{
	B b;
	return 0;
}

Select an option to see the answer and solution.

What is the role of a constructor in classes?

Select an option to see the answer and solution.

How many Destructors are allowed in a Class?

Select an option to see the answer and solution.