Object Oriented Programming using C++ Questions and Answers – Catching Class Types

«
»

This set of Object Oriented Programming (OOPs) using C++ Multiple Choice Questions & Answers (MCQs) focuses on “Catching Class Types”.

1. 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
View Answer

Answer: d
Explanation: 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.
advertisement

2. 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
View Answer

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

3. 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
View Answer

Answer: c
Explanation: 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.
advertisement
advertisement

4. The catching of base class exception ___________________________ in java.
a) After derived class is not allowed by compiler
b) Before derived class is not allowed by compiler
c) Before derived class is allowed
d) After derived class can’t be done
View Answer

Answer: b
Explanation: The catching of base class exception before derived class is not allowed in java. The compiler itself doesn’t allow this declaration. It produces an error.

5. If catching of base class exception is done before derived class in C++ ________________
a) It gives compile time error
b) It doesn’t run the program
c) It may give warning but not error
d) It always gives compile time error
View Answer

Answer: c
Explanation: The compiler in C++ doesn’t identify this as compile time error and allows the execution of the program. But, the compiler may give some warning related to the catch block sequence or code unreachable.
advertisement

6. How many catch blocks can a class have?
a) Only 1
b) 2
c) 3
d) As many as required
View Answer

Answer: d
Explanation: There are many type of exceptions that may arise while running a code. And each catch block can handle only one exception. Hence there can be as many catch blocks as required.

7. Since which version of java is multiple exception catch was made possible?
a) Java 4
b) Java 5
c) Java 6
d) Java 7
View Answer

Answer: d
Explanation: None of the languages used to support multiple exception catch in a single catch block. Since java 7 the feature was added to catch more than one exceptions in one catch block.
advertisement

8. To catch more than one exception in one catch block, how are the exceptions separated in the syntax?
a) Vertical bar
b) Hyphen
c) Plus
d) Modulus
View Answer

Answer: a
Explanation: Just the way we separate the arguments in a function definition using comma. Here we separate the exceptions by using a vertical bar or we call it pipe symbol sometimes. This is just a convention followed to separate different exception list.

9. If a catch block accepts more than one exceptions then __________________
a) The catch parameters are not final
b) The catch parameters are final
c) The catch parameters are not defined
d) The catch parameters are not used
View Answer

Answer: b
Explanation: The catch parameters are made final. This is to ensure that the parameters are not changed inside the catch block. Hence those retain their values.
advertisement

10. Which among the following handles the undefined class in program?
a) ClassNotFound
b) NoClassException
c) ClassFoundException
d) ClassNotFoundException
View Answer

Answer: d
Explanation: It is the exception handler that handles the exceptions when the class used is not found in the program. This is done to handle all the undefined class exceptions. This can be due to a command line error.

11. If classes produce some exceptions, then ______________________
a) Their respective catch block must be defined
b) Their respective catch blocks are not mandatory
c) Their catch blocks should be defined inside main function
d) Their catch blocks must be defined at the end of program
View Answer

Answer: a
Explanation: The catch blocks must be defined. This is to ensure that all the exceptions related to the classes are handled by the program code and the program doesn’t terminate unexpectedly.

12. Which among the following is true?
a) Only the base class catch blocks are important
b) Only the derived class catch blocks are important
c) Both the base and derived class catch blocks are important
d) If base and derived classes both produce exceptions, program doesn’t run
View Answer

Answer: c
Explanation: The purpose of exception handling is to handle the unexpected errors in the program. If base class might produce some error then its catch block must be given and if the derived class might produce some error then it must be given a specific catch block too.

13. Which is the necessary condition to define the base and derived class catch blocks?
a) Base class catch should be defined first
b) Derived class catch should be defined first
c) Catch block for both the classes must not be defined
d) Catch block must be defined inside main function
View Answer

Answer: b
Explanation: The derived class catch blocks must be defined prior to the base class catch block. This is to ensure that all the catch boxes are reachable. If not done, the code might become unreachable which in turn makes the program prone to errors.

14. Only the base class catch box can handle more than one exception in single block.
a) True
b) False
View Answer

Answer: b
Explanation: There is no specific condition that states that only base class catch box can handle more than one exception in single box. Even the derived class catch clocks can handle more than one exceptions.

15. Which condition among the following might result in memory exception?
a) False if conditions
b) Nested if conditions that are all false
c) Infinite loops
d) Loop that runs exactly 99 times
View Answer

Answer: c
Explanation: The infinite loops doesn’t stop running once started. There must be a way to stop the loop but that is always an improper termination. Infinite loops may keep on using more memory and hence would result in memory error.

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.

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

advertisement
advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
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 | Youtube | Instagram | Facebook | Twitter