My Report

C++ Exception Handling Test – 2


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>
#include <string>
#include <cstdlib>
using namespace std;
class A
{
	int a;
    public:
	A(){}
};

class B: public A
{
	int b;
    public:
	B(){}
};

void func()
{
	B b;
	throw b;
}

int main()
{
	try{
		func();
	}
	catch(A a){
		cout<<"Caught A Class\n";
	}
	catch(B b){
		cout<<"Caught B Class\n";
	}
}

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

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class A
{
	int a;
   public:
	A(){}
};

class B: public A
{
	int b;
    public:
	B(){}
};

void func()
{
	B b;
	throw b;
}

int main()
{
	try{
		func();
	}
	catch(B b){
		cout<<"Caught B Class\n";
	}
	catch(A a){
		cout<<"Caught A Class\n";
	}
}

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

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class A
{
	int a;
    public:
	A(){}
};

class B: public A
{
	int b;
    public:
	B(){}
};

void func1()
{
	B b;
	throw b;
}

void func2()
{
	A a;
	throw a;
}

int main()
{
	try{
		func1();
	}
	catch(...){
		cout<<"Caught All types of exceptions\n";
	}
	try{
		func2();
	}
	catch(B b){
		cout<<"Caught All types of exceptions\n";
	}
}

4. An uncaught handler returns to _______________

5. Uncaught exception leads to ______________

6. Where should we place catch block of the derived class in a try-catch block?

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

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class A
{
	int a;
    public:
	A(){}
};
class B: public A
{
	int b;
    public:
	B(){}
};

void func()
{
	B b;
	throw b;
}
int main()
{
	try{
		func();
	}
	catch(B *b){
		cout<<"Caught B Class\n";
	}
	catch(A a){
		cout<<"Caught A Class\n";
	}
}

8. Header file used for exception handling in C++?

9. What id the syntax for catching any type of exceptions?

10. What happens when this C++ program is compiled?

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class A
{
	int a;
    public:
	A(){}
};

class B: public A
{
	int b;
    public:
	B(){}
};

void func()
{
	B b;
	throw b;
}

int main()
{
	try{
		func();
	}
	catch(A a){
		cout<<"Caught A Class\n";
	}
	catch(B b){
		cout<<"Caught B Class\n";
	}
}

 

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.