My Report (&Account)

C++ Constructor and Destructor Test – 3


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

2. Which of the following is correct?

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

4. How destructor overloading is done?

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

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

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

8. When a copy constructor is called?

9. Which of the following constructors are provided by the C++ compiler if not defined in a class?

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

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

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

13. 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<<a1.a<<a2.a;
	return 0;
}

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

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

 

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

advertisement
advertisement
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.