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

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){
  ^~~~~

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

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

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?

advertisement
#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) waits there for some time
View Answer

Answer: c
Explanation: Uncaught handler returns to its callee(i.e. the function it is called by).

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.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn