1. Exceptions are caused by
Explanation:
Correct answer: d) anomalies that a program may encounter during execution
Exceptions in C++ are a way to handle anomalies or exceptional situations that a program may encounter during its execution. These anomalies can be errors or unexpected conditions that cannot be handled in the normal flow of the program. When such anomalies occur, the program can throw an exception, and it can be caught and handled by the appropriate catch blocks.
2. Which of the following events could throw an exception?
Explanation:
Correct answer: d) All of the above
Exceptions can be thrown for various events, including division by zero, accessing array elements out of bounds, and running out of memory space while allocating memory dynamically with "new" keyword. These are just some examples of situations that can trigger an exception in C++.
3. Exceptions are thrown
Explanation:
Correct answer: b) from throw statement to catch block
In C++, exceptions are explicitly thrown using the "throw" statement. When a "throw" statement is encountered in the try block, the control is transferred to the corresponding catch block (if one exists) that can handle the thrown exception. The catch block that matches the type of the thrown exception will catch and handle the exception.
4. State which of the following is TRUE.
Explanation:
Correct answer: b) A statement that causes an exception should be in a try block and d) A program cannot continue to operate after an exception has occurred.
b) A statement that causes an exception should be in a try block: To handle exceptions properly, the statements that may throw an exception should be placed inside a try block. If an exception is thrown within the try block, the program can catch and handle it in one or more catch blocks.
d) A program cannot continue to operate after an exception has occurred: When an exception is thrown and not caught or handled by any catch block, it will terminate the program's normal flow. The program will terminate abnormally, and any code after the point where the exception was thrown will not be executed.
5. The catch block
Explanation:
Correct answer: d) may be placed anywhere in the program
The catch block in C++ can be placed anywhere in the program. It does not have to be immediately after the try block or immediately after the statement throwing the exception. The catch block can be placed at different locations in the program, depending on where you want to handle the exception and what specific actions you want to take when an exception occurs.
6. Which of the following are legal throw statements?
Explanation:
Correct answer: b) throw (exception);
The throw keyword is used to throw an exception from within a method. The exception must be an instance of the Throwable
class or one of its subclasses. The throw statement must be followed by the exception object.
The following are illegal throw statements:
throw ();
This is not a valid exception object.throw exception;
This is not a valid syntax for a throw statement.throw;
This does not specify an exception object to be thrown.
Therefore, the only legal throw statement is throw (exception);
The other options are incorrect for the following reasons:
- Option a) is not a valid exception object.
- Option c) is not a valid syntax for a throw statement.
- Option d) does not specify an exception object to be thrown.
7. When an exception occurs inside a catch block, what happens?
Explanation:
Correct answer: b) The exception is caught by the next catch block in the group
When an exception occurs inside a catch block, the control will not return to the same catch block. Instead, it will try to find the next catch block in the same try / catch sequence that can handle the exception. If there is another catch block that matches the type of the exception, that catch block will catch the exception, and its code will be executed.
8. The statement catch (...) that can catch all types of exception
Explanation:
Correct answer: d) must always be placed at the end of the catch group
The catch block with "(...)" syntax, also known as the catch-all catch block, can catch exceptions of any type. It is typically used as the last catch block in a try / catch sequence to catch any unhandled exceptions that were not caught by the previous catch blocks. Since it can catch exceptions of any type, it must always be placed at the end of the catch group to prevent it from catching exceptions that are intended to be caught by other specific catch blocks.
9. An exception thrown by a try block that is followed by a group of catch blocks,
Explanation:
Correct answer: b) is caught by the catch() whose argument matches with the type of exception thrown
When an exception is thrown in a try block, the program will search for the catch block that matches the type of the thrown exception. If a catch block with a matching type is found in the catch group following the try block, that catch block will catch and handle the exception. It will not check the catch (...) statement first; instead, it will prioritize the specific catch blocks whose argument type matches the type of the thrown exception. If there is no matching catch block in the catch group, then and only then will the catch (...) statement at the end be executed for catching any unhandled exceptions.
10. We may restrict the types of exception thrown from a function
Explanation:
Correct answer: c) by adding a throw (type-list) to the function definition
In C++, you can restrict the types of exception thrown from a function by specifying the exception types in the function definition using the "throw" keyword followed by a type-list in parentheses. The type-list should include the specific exception types that the function is allowed to throw. If the function tries to throw an exception that is not listed in the type-list, the C++ compiler will generate an error.
THE STUDENT FRIENDLY BOOK TO LEARN C++. Let us C++ by Famous author Yashavant Kanetkar Indian writer |
Topic wise C++ MCQs Index ≡
- Principles of Object-Oriented Programming
- Beginning with C++
- Tokens, Expressions, and Control Structures
- Functions in C++
- Classes and Objects
- Constructors and Destructors
- Operator Overloading
- Inheritance
- Pointers, Virtual Functions, and Polymorphism
- Managing Console I/O Operations
- Working with Files
- Templates
- Exception Handling
- Standard Template Library
- Manipulating Strings
- New Features in ANSI C++ Standards
- Object-Oriented System Development
Comments
Post a Comment