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
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
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.
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 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
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.
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
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.
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
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)
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
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
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
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>
Answer: B
Explanation:
: <exception> header file is used to use exception handler in C++.