Java Exception Handling Essentials Quiz

Explore easy questions on exception handling in Java, including checked vs. unchecked exceptions, keywords, and key rules for structuring try/catch blocks.

  1. Definition of an Exception

    What happens when an error occurs within a method in Java?

    1. An exception object is created and handed to the runtime system
    2. The code continues running silently
    3. A warning message is displayed at compile time
    4. The program automatically shuts down

    Explanation: When an error occurs, an exception object containing error details is created and given to the runtime system. The program does not immediately shut down, and warnings may not appear at compile time for exceptions. Ignoring errors is incorrect, as exceptions signal issues needing attention.

  2. Purpose of try/catch Block

    Why would you use a try/catch block in Java programming?

    1. To declare your code is prepared to handle potential exceptions
    2. To speed up code execution
    3. To comment out faulty code
    4. To prevent methods from being called

    Explanation: A try/catch block indicates awareness and preparation for handling exceptions. It does not directly prevent method calls, make code faster, or comment out sections. Its primary use is structured error handling.

  3. Checked vs Unchecked Exceptions

    Which type of exception does the Java compiler check during compilation?

    1. Logic Error
    2. Syntax Error
    3. Unchecked Exception
    4. Checked Exception

    Explanation: Checked exceptions are verified during compilation to ensure the programmer handles them. Unchecked exceptions are not checked by the compiler, and syntax or logic errors are not considered exception types.

  4. Examples of Unchecked Exceptions

    Which of the following is an example of an unchecked exception in Java?

    1. FileNotFoundException
    2. IOException
    3. ClassNotFoundException
    4. NullPointerException

    Explanation: NullPointerException is an unchecked exception, not checked at compile time. The other options are checked exceptions that require handling by the programmer.

  5. Creating and Throwing Exceptions

    Which keyword in Java is used to throw an exception object?

    1. catch
    2. throw
    3. throws
    4. raise

    Explanation: The 'throw' keyword creates and passes an exception to the runtime system. 'throws' declares but does not throw an exception, 'catch' is for handling, and 'raise' is not a Java keyword.

  6. Subclass Hierarchy of Exceptions

    Which Java class is the direct superclass of all exception classes?

    1. Error
    2. RuntimeException
    3. Throwable
    4. Exception

    Explanation: All exception classes directly or indirectly extend the Throwable class. Exception is a subclass of Throwable. RuntimeException and Error are further subclasses, not the top-most superclass.

  7. Valid Structure of try/catch/finally

    Which statement about try/catch/finally structure in Java is true?

    1. You must always use all three blocks together
    2. You cannot have a catch or finally block without a try block
    3. A try block can exist alone without catch or finally
    4. You can write code between try and catch blocks

    Explanation: A catch or finally block must follow a try block, making the first statement correct. You cannot insert code between try and catch, and a try block must have either catch or finally, though not necessarily both. Using all three at once is not mandatory.

  8. Meaning of an Unchecked Exception

    What characterizes an unchecked exception in Java?

    1. It must be declared with 'throws'
    2. It cannot be caught by a catch block
    3. It is always fatal to the program
    4. It is not checked by the compiler during compilation

    Explanation: Unchecked exceptions are not verified by the compiler and do not require explicit handling or declaration. They can still be caught, and they're not always fatal, nor must they be declared using 'throws'.