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

3/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>
#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{
	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.

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

Select an option to see the answer and solution.

How destructor overloading is done?

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

Select an option to see the answer and solution.

When destructors are called?

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{
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 is the difference between constructors and destructors?

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

Select an option to see the answer and solution.

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

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<<"Constructor called";
	}
};
int main(int argc, char const *argv[])
{
	A 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";
	}
};
int main(int argc, char const *argv[])
{
	A *a1 = (A*)malloc(sizeof(A));
	return 0;
}

Select an option to see the answer and solution.

Which of the following constructors are provided by the C++ compiler if not defined in 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:
	A(){
		cout<<"Constructor called";
	}
};
int main(int argc, char const *argv[])
{
	A *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(int a=0){
		this->a = a;
	}
};
int main(int argc, char const *argv[])
{
	A a1, a2(10);
	cout<<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";
	}
} a;
int main(int argc, char const *argv[])
{
	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{
	mutable int a;
public:
	A(){
		cout<<"A's Constructor called\n";
	}
	~A(){
		cout<<"A's Destructor called\n";
	}
};
 
class B{
	static A a;
public:
	B(){
		cout<<"B's Constructor called\n";
	}
	~B(){
		cout<<"B's Destructor called\n";
	}
};
 
A B::a;
 
int main(int argc, char const *argv[])
{
	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(int a){
		this->a = a;
	}
};
int main(int argc, char const *argv[])
{
	A a1, a2(10);
	cout<<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>
#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;
}

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.