1.
An exception is:
Correct Answer
B. A problem that a program has during runtime
Explanation
The correct answer is "a problem that a program has during runtime." An exception refers to an unexpected issue or error that occurs while a program is running. It is a deviation from the normal flow of execution and can cause the program to terminate or behave in an unintended way. Exceptions can be caused by various factors such as invalid input, memory errors, or programming mistakes. They are typically handled by the program through exception handling mechanisms to prevent crashes and allow for graceful error recovery.
2.
In many cases, handling an exception allows a program to automatically restart the execution of the program from the beginning.
Correct Answer
B. False
Explanation
False. In many cases, handling an exception allows a program to continue executing as if no problem had been encountered.
3.
The base class for all exception classes is System. Exception
Correct Answer
A. True
Explanation
The statement is true because in C#, the base class for all exception classes is System.Exception. This means that all exception classes in C# inherit from the System.Exception class, allowing them to have common properties and methods. By having a common base class, it becomes easier to handle and manage exceptions in a consistent manner throughout the code.
4.
Which of the following is not included in an exception's stack trace?
Correct Answer
D. Instructions on handling the exception.
Explanation
The stack trace of an exception typically includes information about the method-call stack at the time the exception occurred, the name of the exception, and a descriptive message for the exception. However, it does not include instructions on handling the exception. The stack trace is primarily used for debugging purposes, providing a detailed report of the sequence of method calls that led to the exception. Instructions on handling the exception would typically be provided separately in the form of documentation or code comments.
5.
Which of the following is false regarding the throw point of an exception?
Correct Answer
A. It specifies the point at which the exception must be handled.
Explanation
The throw point of an exception is not the point at which the exception must be handled. Instead, it is the initial point at which the exception occurs. It is specified as the top row of the method-call stack at the time the exception occurred. Therefore, the statement "It specifies the point at which the exception must be handled" is false.
6.
After an exception has occurred and a stack trace has been printed, the program may exit or continue executing, depending on the circumstances.
Correct Answer
A. True
Explanation
After an exception has occurred and a stack trace has been printed, the program may exit or continue executing, depending on the circumstances. This is because when an exception occurs, the program flow is disrupted and the exception is propagated up the call stack until it is caught and handled by an appropriate exception handler. If the exception is not caught and handled, the program may terminate abruptly. However, if there is a catch block or an exception handler that can handle the exception, the program can continue executing after printing the stack trace.
7.
In the catch block below, what is e?catch ( DivideByZeroException e ){Console.WriteLine( e );} // end catch
Correct Answer
B. The name of the catch block's exception parameter
Explanation
The catch block is used to handle exceptions that occur in the try block. In this case, the catch block is catching a DivideByZeroException. The variable "e" is the exception parameter, which is used to refer to the exception object that is caught. It allows the programmer to access information about the exception, such as its type, message, and stack trace. Therefore, in this catch block, "e" represents the DivideByZeroException that was thrown.
8.
A catch block that does not specify an exception type or an identifier _______.
Correct Answer
C. Can catch any exceptions
Explanation
A catch block that does not specify an exception type or an identifier can catch any exceptions. This means that it will be able to handle and process any type of exception that may occur within the corresponding try block. By not specifying a specific exception type or identifier, the catch block becomes a general exception handler, capable of catching and handling any type of exception that is thrown.
9.
Which of the following statements about try blocks is true?
Correct Answer
D. The try block should contain statements that may throw an exception.
Explanation
The try block should contain statements that may throw an exception. This is because the purpose of a try block is to enclose a section of code that may potentially throw an exception. If an exception occurs within the try block, it can be caught and handled by a catch block. If no exception is thrown, the catch block is skipped and the program continues execution after the try-catch construct. Therefore, it is important to include statements that may potentially throw an exception within the try block.
10.
What's the difference between a try block and a try statement?
Correct Answer
C. The try block refers to the keyword try followed by a block of code. The try block and its corresponding catch and/or finally clauses together form a try statement.
Explanation
The correct answer explains that the try block refers to the keyword try followed by a block of code, and it is the combination of the try block and its corresponding catch and/or finally clauses that form a try statement. This answer accurately distinguishes between the two terms and clarifies their relationship.
11.
In order to display the error message generated by an exception, you use:
Correct Answer
B. The Message property of class Exception
Explanation
To display the error message generated by an exception, you would use the Message property of the Exception class. This property contains the error message associated with the exception and can be accessed to retrieve and display the message. The other options mentioned (Message method, ErrorMessage method, and ErrorMessage property) do not exist in the Exception class and therefore would not be used for this purpose.
12.
Exception handlers typically access objects in their try blocks to determine the causes of the exceptions.
Correct Answer
B. False
Explanation
False. An exception handler cannot access objects declared within its try block because the try block has expired when the catch clause begins executing.
13.
Usercreated exceptions can be created by:
Correct Answer
C. Extending class Exception.
Explanation
User-created exceptions can be created by extending the class Exception. In object-oriented programming, extending a class means creating a new class that inherits the properties and behaviors of the parent class. By extending the Exception class, developers can create their own custom exceptions that can be thrown and caught in their code. This allows for more specific and meaningful error handling in applications.
14.
A NullReferenceException is thrown when there's an attempt to use a reference that points to nothing.
Correct Answer
A. True
Explanation
A NullReferenceException is a type of exception that occurs when a program tries to use a reference variable that is not referencing any object. In other words, it is an attempt to access a member or perform an operation on an object that is null (has no value). This can happen when a variable is not properly initialized or when it is explicitly set to null. The correct answer is true because a NullReferenceException is indeed thrown when there is an attempt to use a reference that points to nothing.
15.
There must be a catch clause for every expected exception type.
Correct Answer
B. False
Explanation
False, catch clauses for exception classes that are the base classes of other exception classes can be used; these will catch all exceptions that inherit from the base class. Using class Exception will catch all exceptions. If there is no corresponding catch clause, the program will terminate.
16.
Which of the following statements is false?
Correct Answer
C. The final block and try block can appear in any order.
Explanation
The final block and try block cannot appear in any order. In Java, the final block must always come after the try block. The final block is used to release resources acquired in the try block, ensuring that they are properly cleaned up regardless of whether an exception is thrown or not. Therefore, the statement "The final block and try block can appear in any order" is false.
17.
After a final block has finished executing:
Correct Answer
A. Control proceeds to the first statement after the final block.
Explanation
After a final block has finished executing, control proceeds to the first statement after the final block. This means that the program will continue executing the code that comes after the final block. The final block is typically used for cleaning up resources or performing any necessary actions before the program terminates or moves on to the next section of code.
18.
In order to tell the user what happened in an exception you must
Correct Answer
C. Access Exception properties
Explanation
To inform the user about what happened in an exception, you need to access the properties of the exception. This allows you to retrieve information such as the type of exception, error message, stack trace, and any other relevant details. By accessing these properties, you can provide a more informative and helpful explanation to the user regarding the exception that occurred.
19.
After the last catch block, the required final block provides code that always executes regardless of whether or not an exception occurs.
Correct Answer
B. False
Explanation
False. The final block is optional.
20.
The final block is an ideal location for code that releases resources to prevent "resource leaks."
Correct Answer
A. True
Explanation
The final block in a code is an ideal location for releasing resources to prevent "resource leaks" because it ensures that the resources are always released, regardless of whether an exception is thrown or not. By placing the resource release code in the final block, it guarantees that the resources will be properly cleaned up and prevents any potential memory leaks or other issues that could occur if the resources are not released correctly.
21.
The final block is executed only if no error was reached in the try block.
Correct Answer
B. False
Explanation
False, the final block will execute no matter what; error or no error.
22.
There can be no code in between try/catch/final blocks.
Correct Answer
A. True
Explanation
The statement is true because the try/catch/finally blocks are used to handle exceptions in a program. These blocks are designed to catch any exceptions that occur within the try block and execute the appropriate catch block. The finally block is used to execute code that should always run, regardless of whether an exception occurred or not. Since these blocks are meant to handle exceptions, it is not possible to have code in between them as it would not be properly handled in case of an exception.
23.
Which of the following is not a property of Exception?
Correct Answer
D. PrintError
24.
Stack unwinding is the process that attempts to locate an appropriate catch handler for an uncaught exception.
Correct Answer
A. True
Explanation
Stack unwinding is the process by which the program searches for an appropriate catch handler to handle an uncaught exception. When an exception is thrown, the program looks for a catch block that can handle the exception. If a suitable catch block is found, the program jumps to that block and executes the appropriate code. If no catch block is found, the program continues to unwind the stack, searching for a catch block in higher-level functions. If no catch block is found at all, the program terminates and the exception remains unhandled. Therefore, the statement is true.
25.
Userdefined exceptions should be derived from the:
Correct Answer
C. Exception class.
Explanation
User-defined exceptions should be derived from the Exception class. The Exception class is the base class for all exceptions in Java. When creating a custom exception, it is recommended to extend the Exception class to ensure that it follows the standard exception hierarchy and can be caught and handled properly by the program. ApplicationException, ConsoleException, and SystemException are not standard exception classes in Java, so they should not be used as base classes for user-defined exceptions.