Java Exception Handling - II

Approved & Edited by ProProfs Editorial Team
The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
Learn about Our Editorial Process
| By Dthakker11
D
Dthakker11
Community Contributor
Quizzes Created: 3 | Total Attempts: 2,070
Questions: 6 | Attempts: 298

SettingsSettingsSettings
Java Exception Handling - II - Quiz


Questions and Answers
  • 1. 

    Which of the following are true about Exceptions in the Java language?

    • A.

      They are only thrown when a user makes a mistake while using your program

    • B.

      All types of exceptions are instances of the same single class

    • C.

      They must be caught or declared to be thrown by your methods

    • D.

      Compiler helps with all types of exceptions

    Correct Answer
    C. They must be caught or declared to be thrown by your methods
    Explanation
    Exceptions in Java are implemented as objects and must be either caught or thrown by your methods when you call a method that can throw Exceptions. Exceptions can be instances of many different classes e.g. IOException, SQLException, NullPointerException.

    Rate this question:

  • 2. 

    There is a problem in the syntax of the regular expression pattern you have written. What exception will be thrown?

    • A.

      IncorrectRegExPatternException

    • B.

      IncorrectPatternException

    • C.

      InvalidPattternException

    • D.

      PatternSyntaxException

    Correct Answer
    D. PatternSyntaxException
    Explanation
    The correct answer is PatternSyntaxException. This exception is thrown when there is a problem in the syntax of a regular expression pattern. It indicates that the pattern is invalid or contains errors that prevent it from being parsed correctly.

    Rate this question:

  • 3. 

    When handling exceptions within programs why might a programmer define multiple catch blocks for a single try block?

    • A.

      Programs will not compile unless at least two catch blocks are defined

    • B.

      Try blocks do not allow multiple catch blocks to be defined

    • C.

      To handle different kinds of Exceptions in different ways

    • D.

      To demonstrate their advanced coding ability

    Correct Answer
    C. To handle different kinds of Exceptions in different ways
    Explanation
    When handling exceptions within programs, a programmer might define multiple catch blocks for a single try block to handle different kinds of exceptions in different ways. By having multiple catch blocks, the programmer can specify different exception types and provide specific handling for each type of exception. This allows for more precise and tailored error handling, improving the robustness and reliability of the program.

    Rate this question:

  • 4. 

    In case of multiple catch blocks in a Java program

    • A.

      The superclass exception is caught first

    • B.

      The superclass exception is not caught first

    • C.

      The most specific exception to the error is caught first

    • D.

      None of these

    Correct Answer(s)
    B. The superclass exception is not caught first
    C. The most specific exception to the error is caught first
    Explanation
    In case of multiple catch blocks in a Java program, the most specific exception to the error is caught first. This means that if there are multiple catch blocks for different exceptions, the catch block for the exception that is the most specific will be executed first. This is because Java follows an inheritance hierarchy for exceptions, where more specific exceptions are subclasses of more general exceptions. Therefore, if an exception occurs, Java will try to find the catch block that matches the most specific exception type before moving on to more general catch blocks.

    Rate this question:

  • 5. 

    Is the following code legal?   try { } finally { }

    • A.

      Yes

    • B.

      No

    Correct Answer
    A. Yes
    Explanation
    Yes, it's legal — and very useful A try statement does not have to have a catch block if it has a finally block. If the code in the try statement has multiple exit points and no associated catch clauses, the code in the finally block is executed no matter how the try block is exited. Thus it makes sense to provide a finally block whenever there is code that must always be executed. This include resource recovery code, such as the code to close I/O streams.

    Rate this question:

  • 6. 

    Will this code compile?   try { } catch (Exception e) { } catch (ArithmeticException a) { }

    • A.

      Yes

    • B.

      No

    Correct Answer
    B. No
    Explanation
    This first handler catches exceptions of type Exception; therefore, it catches any exception, including ArithmeticException. The second handler could never be reached. This code will not compile.

    Rate this question:

Quiz Review Timeline +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Mar 21, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Feb 06, 2018
    Quiz Created by
    Dthakker11
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.