250+ TOP MCQs on Exception Handling – 1 and Answers

C++ Programming Multiple Choice Questions & Answers (MCQs) on “Exception Handling – 1”.

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
Answer: a
Clarification: 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
Answer: b
Clarification: 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
Answer: a
Clarification: 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.

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
Answer: b
Clarification: 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....
}

d)

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

View Answer

Answer: a
Clarification: 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…..

 
 

6. Which part of the try-catch block is always fully executed?
a) try part
b) catch part
c) finally part
d) throw part
Answer: c
Clarification: 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
Answer: a
Clarification: 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
Answer: a
Clarification: 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
Answer: c
Clarification: 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
Answer: b
Clarification: 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
Answer: b
Clarification: ‘throw’ keyword is used to throw exceptions if something bad happens.

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

#include 
#include 
#include 
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
Answer: c
Clarification: 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 
#include 
#include 
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
Answer: b
Clarification: 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
Answer: d
Clarification: 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 
#include 
#include 
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 zeron";
			else
				throw "value of b is less than zeron";
		}
	}
	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)
Answer: b
Clarification: 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

Leave a Reply

Your email address will not be published. Required fields are marked *