Test Series - oops

Test Number 59/78

Q: 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
Solution: 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.
Q: 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
Solution: 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.
Q: Which among the following is true for class exceptions?
A. Only base class can give rise to exceptions
B. Only derived class can give rise to exceptions
C. Either base class or derived class may produce exceptions
D. Both base class and derived class may produce exceptions
Solution: It’s not mandatory that either base class or derived class can give rise to exceptions. The exceptions might get produced from any class. The exceptions depends on code.
Q: If both base and derived class caught exceptions ______________
A. Then catch block of derived class must be defined before base class
B. Then catch block of base class must be defined before the derived class
C. Then catch block of base and derived classes doesn’t matter
D. Then catch block of base and derived classes are not mandatory to be defined
Solution: It is a condition for writing the catch blocks for base and derived classes. It is mandatory to write derived class catch block first because the errors produced by the derived class must be handled first.
Q: 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
Solution: 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.
Q: Which among the following is true?
A. If catch block of base class is written first, it is compile time error
B. If catch block of base class is written first, it is run time error
C. If catch block of base class is written first, derived class catch block can’t be reached
D. If catch block of base class is written first, only derived class catch block is executed
Solution: If the catch block of the base class is defined first and then the derived class catch block is given. The code becomes unreachable. Hence the derived class catch block must be written first.
Q: 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
Solution: 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.
Q: Which symbol should be used to separate the type of exception handler classes in a single catch block?
A. ?
B. ,
C. 
D. |
Solution: 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.
Q: Which class is used to handle the input and output exceptions?
A. InputOutput
B. InputOutputExceptions
C. IOExceptions
D. ExceptionsIO
Solution: 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.
Q: 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()
Solution: 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.

You Have Score    /10