C++ Programming MCQ – Exception Handling

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

1. What is an exception in C++ program?
a) A problem that arises during the execution of a program
b) A problem that arises during compilation
c) Also known as the syntax error
d) Also known as semantic error
View Answer

Answer: a
Explanation: An exception is defined as the problem in C++ program that arises during the execution of the program for example divide by zero error.

2. By default, what a program does when it detects an exception?
a) Continue running
b) Results in the termination of the program
c) Calls other functions of the program
d) Removes the exception and tells the programmer about an exception
View Answer

Answer: b
Explanation: By default, whenever a program detects an exception the program crashes as it does not know how to handle it hence results in the termination of the program.

3. Why do we need to handle exceptions?
a) To avoid unexpected behaviour of a program during run-time
b) To let compiler remove all exceptions by itself
c) To successfully compile the program
d) To get correct output
View Answer

Answer: a
Explanation: We need to handle exceptions in a program to avoid any unexpected behaviour during run-time because that behaviour may affect other parts of the program. Also, an exception is detected during run-time, therefore, a program may compile successfully even with some exceptions cases in your program.
advertisement
advertisement

4. How Exception handling is implemented in the C++ program?
a) Using Exception keyword
b) Using try-catch block
c) Using Exception block
d) Using Error handling schedules
View Answer

Answer: b
Explanation: C++ provides a try-catch block to handle exceptions in your program.

5. What is the correct syntax of the try-catch block?
a)

try
{
	// programable codes.....
}
catch(Exceptions)
{
	// Code for handling exceptions....
}

b)

try()
{
	// programable codes.....
}
catch(Exceptions)
{
	// Code for handling exceptions....
}

c)

try
{
	// programable codes.....
}
catch
{
	// Code for handling exceptions....
}
advertisement

d)

try()
{
	// programable codes.....
}
catch
{
	// Code for handling exceptions....
}
View Answer
Answer: a
Explanation: Try-catch block has the following syntax:

try{
	// codes that needs to check for exceptions
}
catch(Exception E1){
      // codes for handling exception.... 
      // Exception E denotes the type of exception this block is handling.
}
catch(Exception E2){
	// other exception that needs to be handled...
}

You can have any number of catch blocks catching different exceptions…..

 
 

advertisement

6. Which part of the try-catch block is always fully executed?
a) try part
b) catch part
c) finally part
d) throw part
View Answer

Answer: c
Explanation: finally part of the try-catch block is always executed whether exceptions are caught or not.

7. Which of the following is an exception in C++?
a) Divide by zero
b) Semicolon not written
c) Variable not declared
d) An expression is wrongly written
View Answer

Answer: a
Explanation: Exceptions are those which are encountered during run-time of the program. semicolon, variable not declared and the wrong expression are compile-time errors, therefore, they are not exceptions. Divide by zero is the problem that is encountered during run-time, therefore, it is an exception.

8. What is an error in C++?
a) Violation of syntactic and semantic rules of a languages
b) Missing of Semicolon
c) Missing of double quotes
d) Violation of program interface
View Answer

Answer: a
Explanation: An error occurs when rules and laws of a language is violated while writing programs in that language.

9. What is the difference between error and exception?
a) Both are the same
b) Errors can be handled at the run-time but the exceptions cannot
c) Exceptions can be handled at the run-time but the errors cannot
d) Both can be handled during run-time
View Answer

Answer: c
Explanation: Exceptions can be handled during run-time whereas errors cannot be because exceptions occur due to some unexpected conditions during run-time whereas about errors compiler is sure and tells about them during compile-time.

10. What are the different types of exceptions?
a) 1
b) 2
c) 3
d) 4
View Answer

Answer: b
Explanation: There are two types of exceptions: Synchronous and asynchronous exceptions. Synchronous exceptions that are caused by the event which can be controlled by the program whereas Asynchronous exceptions are those which are beyond the control of the program.

11. Which keyword is used to throw an exception?
a) try
b) throw
c) throws
d) except
View Answer

Answer: b
Explanation: ‘throw’ keyword is used to throw exceptions if something bad happens.

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

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
 
void func(int a, int b)
{
 
	if(b == 0){
		throw "This value of b will make the product zero. " 
                      "So please provide positive values.\n";
	}
	else{
		cout<<"Product of "<<a<<" and  "<<b<<" is: "<<a*b<<endl;
	}
}
 
int main()
{
	try{
		func(5,0);
	}
	catch(const char* e){
		cout<<e;
	}
}

a) 0
b) 5
c) This value of b will make the product zero. So please provide positive values.
d) Product of 5 and 0 is: 0
View Answer

Answer: c
Explanation: As the value of b = 0 is provided to the func() and the function is throwing an exception whenever the value of b = 0. Therefore the function throws the exception which will be printed on the screen.
Output:

$ ./a.out 
This value of b will make the product zero. So please provide positive values.

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

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
void func(int a, int b)
{
	if(b == 0){
		throw "This value of b will make the product zero. " 
                       "So please provide positive values.\n";
	}
	else{
		cout<<"Product of "<<a<<" and  "<<b<<" is: "<<a*b<<endl;
	}
}
 
int main()
{
	try{
		func(5,0);
	}
	catch(char* e){
		cout<<e;
	}
}

a) 0
b) Aborted (core dumped)
c) This value of b will make the product zero. So please provide positive values.
d) Product of 5 and 0 is: 0
View Answer

Answer: b
Explanation: As the func() is throwing a const char* string but we the catch block is not catching any const char* exception i.e. exception thrown is not handled therefore the program results into Aborted(core dumped).
Output:

$ ./a.out 
terminate called after throwing an instance of 'char const*'
Aborted (core dumped)

14. What is Re-throwing an exception means in C++?
a) An exception that is thrown again as it is not handled by that catching block
b) An exception that is caught twice
c) An exception that is not handled in one caught hence thrown again
d) All of the mentioned
View Answer

Answer: d
Explanation: Exception that is caught by a catch block but not handled by that catch block can be re-thrown by that catch block to further try-catch block.

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

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
void func(int a, int b)
{
	if(b < 1){
		throw b;
	}
	else{
		cout<<"Product of "<<a<<" and  "<<b<<" is: "<<a*b<<endl;
	}
}
 
int main()
{
	try
        {
		try
                {			
		    func(5,-1);
		}
		catch(int b)
                {
			if(b==0)
				throw "value of b is zero\n";
			else
				throw "value of b is less than zero\n";
		}
	}
	catch(const char* e)
        {
		cout<<e;
	}
 
}

a) value of b is zero
b) value of b is less than zero
c) Product of 5 and -1 is: -5
d) Aborted(core dumped)
View Answer

Answer: b
Explanation: Here the func() throws the value of b which is caught by the inner try-catch block, which again throws the message inorder to handle different cases of b which is caught by the outer try-catch block. Now as the value of b is negative the program outputs the message as shown.
Output:

$ ./a.out 
value of b is less than zero

More Exception Handling MCQs in C++ Programming:

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.