C++ Programming Questions and Answers – Exception Handling – 2

This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “Exception Handling – 2”.

1. Where should we place catch block of the derived class in a try-catch block?
a) Before the catch block of Base class
b) After the catch block of Base class
c) Anywhere in the sequence of catch blocks
d) After all the catch blocks
View Answer

Answer: a
Explanation: C++ asks the programmer to place the catch block of derived class before a catch block of the base class, otherwise derived catch block will never be executed.

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

a) The program compiles successfully without any errors or warnings
b) Compile-time error occurs
c) The program compiles successfully with warnings
d) The program gives both errors and warnings
View Answer

Answer: c
Explanation: Catch block of derived should always be placed before the catch block base class, hence the program gives warnings stating that exceptions of the derived class will be caught by the base class.
Output:

$ g++ check.cpp
check.cpp: In function ‘int main()’:
check.cpp:33:2: warning: exception of type ‘B’ will be caught
  catch(B b){
  ^~~~~
check.cpp:30:2: warning:    by earlier handler for ‘A’
  catch(A a){
  ^~~~~
advertisement
advertisement

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

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
#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";
	}
}

a) Caught B Class
b) Caught A Class
c) Compile-time error
d) Run-time error
View Answer

Answer: b
Explanation: As the catch block of the derived class is after the catch block of base class, therefore, all the exceptions of the derived class will be caught by the base class, Hence the output of catch block of class A is printed.
Output:

$ ./a.out 
Caught A Class
advertisement

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

a) Caught B Class
b) Caught A Class
c) Compile-time error
d) Run-time error
View Answer

Answer: a
Explanation: In this as the catch block of the derived class is before the catch block of the base class so when func() throws the object of class B it is caught by the catch block of class B, Hence the output is printed as shown.
Output:

$ ./a.out 
Caught B Class
advertisement

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

a) Caught B Class
b) Caught A Class
c) Compile-time error
d) Run-time error
View Answer

Answer: b
Explanation: The func() throws the object of class B but as catch block is defined to catch the exception of class B, Therefore the exception is caught by the base class A. The programmer has defined the catch block for B*, therefore, the object B is not caught by the pointer object B*.

6. What id the syntax for catching any type of exceptions?
a) catch(Exception e)
b) catch(…)
c) catch(Exception ALL)
d) catch(ALL)
View Answer

Answer: b
Explanation: catch(…) is used in C++ to catch all types of exceptions in a single 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 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";
	}
}

a) Caught All types of exceptions
b)

Caught All types of exceptions
Aborted (core dumped)

c) Error
d)

Caught All types of exceptions
Caught All types of exceptions
View Answer
Answer: b
Explanation: Two try-catch blocks is declared each catching the respective exceptions from class A and B. But as we have defined catch all exceptions in the first case, therefore, the exception for class B is caught when thrown by the func1(), but in the second case, the try-catch block is catching only the exception for class B so when func2() throws class A exception and no catch block to catch that exception therefore program results into abort(core dumped).
 
 

8. Uncaught exception leads to ______________
a) termination of program
b) successful execution of programs
c) no effect on the program
d) execution of other functions of the program starts
View Answer

Answer: a
Explanation: Uncaught exceptions in a program leads to the termination of a program.

9. An uncaught handler returns to _______________
a) main function
b) its caller
c) its callee
d) none of the mentioned
View Answer

Answer: d
Explanation: Uncaught exceptions do not “return” to any specific location in the program. They trigger a chain of events leading to program termination. In C++, when an uncaught exception occurs, the program unwinds the stack to find an appropriate exception handler. It searches backward through the call stack (i.e., the series of function calls that led to the point where the exception occurred) until it finds a function that has a suitable exception handler. If no handler is found, the program may terminate or exhibit undefined behavior.

10. Header file used for exception handling in C++?
a) <cstdlib>
b) <string>
c) <handler>
d) <exception>
View Answer

Answer: d
Explanation: <exception> header file is used to use exception handler in C++.

Sanfoundry Global Education & Learning Series – C++ Programming Language.

To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
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.