This set of Object Oriented Programming (OOPs) using C++ Multiple Choice Questions & Answers (MCQs) focuses on “Exception Handling”.
1. What is an exception?
a) Problem arising during compile time
b) Problem arising during runtime
c) Problem in syntax
d) Problem in IDE
Explanation: The problems that might occur during execution of a program are known as exceptions. The exceptions are unexpected sometimes and can be predicted. Also, the exceptions should be always considered for a better program.
2. Why do we need to handle exceptions?
a) To prevent abnormal termination of program
b) To encourage exception prone program
c) To avoid syntax errors
d) To save memory
Explanation: The exceptions should be handled to prevent any abnormal termination of a program. The program should keep running even if it gets interrupted in between. The program should preferable show the error occurred and then retry the process or just continue the program further.
3. An exception may arise when _______________
a) Input is fixed
b) Input is some constant value of program
c) Input given is invalid
d) Input is valid
Explanation: The exceptions may arise because the input given by the user might not be of the same type that a program can manage. If the input is invalid the program gets terminated.
4. If a file that needs to be opened is not found in the target location then _____________
a) Exception will be produced
b) Exceptions are not produced
c) Exception might get produced because of syntax
d) Exceptions are not produced because of logic
Explanation: The exceptions are produced when anything unexpected happened. The program might not be able to find a file in the target location and hence program produces an exceptions. The exception produced, then terminates the program.
5. Which is the universal exception handler class?
Explanation: Any type of exception can be handled by using class Exceptions. An object of this class is created which can manipulate the exception data. The data can be used to display the error or to run the program further based on error produced.
6. What are two exception classes in hierarchy of java exceptions class?
a) Runtime exceptions only
b) Compile time exceptions only
c) Runtime exceptions and other exceptions
d) Other exceptions
Explanation: The exceptions class is having two other derived classes which are of runtime exception handler and for other type of exceptions handling. The runtime exception handler is used to handle the exceptions produced during run time and same with case of other exceptions.
7. Which are the two blocks that are used to check error and handle the error?
a) Try and catch
b) Trying and catching
c) Do and while
d) TryDo and Check
Explanation: Two blocks that are used to check for errors and to handle the errors are try and catch block. The code which might produce some exceptions is placed inside the try block and then the catch block is written to catch the error that is produced. The error message or any other processing can be done in catch block if the error is produced.
8. There can be a try block without catch block but vice versa is not possible.
Explanation: The try block may or may not have any catch block. But a catch block can’t be there in a program if there is no try block. It is like else-block can only be written if and only if if-block is present in the program.
9. How many catch blocks can a single try block can have?
a) Only 1
b) Only 2
c) Maximum 127
d) As many as required
Explanation: There is no limit on the number of catch blocks corresponding to a try block. This is because the error can be of any type and for each type, a new catch block can be defined. This is to make sure all type of exceptions can be handled.
10. Which among the following is not a method of Throwable class?
a) public String getMessage()
b) public Throwable getCause()
c) public Char toString()
d) public void printStackTrace()
Explanation: Actually all the functions are available in throwable class. But the return type given in the option is wrong. The function toString returns string value. Hence the return type must be a String and not a char.
11. To catch the exceptions ___________________
a) An object must be created to catch the exception
b) A variable should be created to catch the exception
c) An array should be created to catch all the exceptions
d) A string have to be created to store the exception
Explanation: The object must be created of a specific class of which the error has occurred. If the type of error is unknown then we can use an object of class Exceptions. This object will be able to handle any kind of exception that a program might produce.
12. Multiple catch blocks __________________
a) Are mandatory for each try block
b) Can be combined into a single catch block
c) Are not possible for a try block
d) Can never be associated with a single try block
Explanation: The separate catch blocks for a single try block can be combined into a single catch block. All type of errors can be then handled in s single block. The type still have to be specified for the errors that might be produced.
13. Which symbol should be used to separate the type of exception handler classes in a single catch block?
Explanation: A pipe symbol can be used to separate different type of exceptions. The exceptions should always be given in proper sequence to ensure that no code remains unreachable. If not done properly the code might never be used in a program.
14. Which class is used to handle the input and output exceptions?
Explanation: There is a specific class to handle each type of exceptions that might be produced in a program. The input and output exceptions can be handled by an object of class IOExcceptions. This class handles all type of input and output exceptions.
15. Why do we use finally block?
a) To execute the block if exception occurred
b) To execute a code when exception is not occurred
c) To execute a code whenever required
d) To execute a code with each and every run of program
Explanation: Sometimes there is a need to execute a set of code every time the program runs. Even if the exception occurs and even if it doesn’t, there can be some code that must be executed at end of the program. That code is written in finally block. This block is always executed regardless of exceptions occurring.
Sanfoundry Global Education & Learning Series – Object Oriented Programming (OOPs).
To practice all areas of Object Oriented Programming (OOPs) using C++, here is complete set of 1000+ Multiple Choice Questions and Answers.