Exception Handling in Java Exercise I


Ques 1 Exception Handling


Which keyword is used to declare an exception handler in Java?
  a) try
  b) catch
  c) handle
  d) throw


b is the correct option




Ques 2 Exception Handling


Which keyword is used to explicitly throw an exception in Java?
  a) try
  b) catch
  c) finally
  d) throw


d is the correct option




Ques 3 Exception Handling


What is the purpose of the "finally" block in Java exception handling?
  a) To handle exceptions that occur in the try block.
  b) To provide a place for code that always executes, whether an exception is thrown or not.
  c) To specify the type of exception to catch.
  d) To re-throw an exception that was caught earlier.


b is the correct option




Ques 4 Exception Handling


Which exception class is the superclass for all built-in exceptions in Java?
  a) Exception
  b) Throwable
  c) Error
  d) RuntimeException


b is the correct option




Ques 5 Exception Handling


Which of the following statements is true about checked exceptions in Java?
  a) Checked exceptions are subclasses of the RuntimeException class.
  b) Checked exceptions are also known as unchecked exceptions.
  c) Checked exceptions must be caught or declared using the "throws" keyword.
  d) Checked exceptions are automatically caught by the JVM.


c is the correct option




Ques 6 Exception Handling


What is the output of the following Java Code?

try {
    int result = 10 / 0;
    System.out.println(result);
} catch (ArithmeticException e) {
    System.out.println("Error: " + e.getMessage());
}

  a) Error: / by zero
  b) 0
  c) Compilation error
  d) ArithmeticException


a is the correct option




Ques 7 Exception Handling


What is the output of the following Java Code?

try {
    String str = null;
    System.out.println(str.length());
} catch (NullPointerException e) {
    System.out.println("NullPointerException: " + e.getMessage());
} catch (Exception e) {
    System.out.println("Exception: " + e.getMessage());
}

  a) Exception
  b) NullPointerException: null
  c) Compilation error
  d) NullPointerException


d is the correct option