AllInfoHub Logo

AllInfoHub – MCQ Practice

Exception Handling – Multiple Choice Questions (MCQs)

  1. 25. What is the output of the following code? `try { int result = 10 / 0; System.out.println(\Result: \" + result); } catch (ArithmeticException e) { System.out.println(\""Caught ArithmeticException\""); }`"""

    • A. Result: ...
    • B. Caught ArithmeticException
    • C. Error
    • D. None
  2. 26. What is the output of the following code? `try { int[] arr = new int[5]; System.out.println(arr[10]); } catch (ArrayIndexOutOfBoundsException e) { System.out.println(\Caught ArrayIndexOutOfBoundsException\"); }`"""

    • A. 0
    • B. Caught ArrayIndexOutOfBoundsException
    • C. Error
    • D. None
  3. 27. What is the output of the following code? `try { String s = null; System.out.println(s.length()); } catch (NullPointerException e) { System.out.println(\Caught NullPointerException\"); }`"""

    • A. null
    • B. 0
    • C. Caught NullPointerException
    • D. Error
  4. 28. What is the output of the following code? `try { System.out.println(\Try block\"); } finally { System.out.println(\""Finally block\""); }`"""

    • A. Try block
    • B. Finally block
    • C. Try block\nFinally block
    • D. Error
  5. 29. What is the output of the following code? `try { int result = 5 / 1; System.out.println(\Result: \" + result); } catch (ArithmeticException e) { System.out.println(\""Caught ArithmeticException\""); } finally { System.out.println(\""Finally block\""); }`"""

    • A. Result: 5
    • B. Caught ArithmeticException\nFinally block
    • C. Result: 5\nFinally block
    • D. Error
  6. 30. What is the output of the following code? `try { int result = 5 / 0; System.out.println(\Result: \" + result); } catch (ArithmeticException e) { System.out.println(\""Caught ArithmeticException\""); } finally { System.out.println(\""Finally block\""); }`"""

    • A. Caught ArithmeticException
    • B. Result: ...\nFinally block
    • C. Caught ArithmeticException\nFinally block
    • D. Error
  7. 31. What is the output of the following code? `try { throw new Exception(\Generic exception\"); } catch (Exception e) { System.out.println(\""Caught Exception: \"" + e.getMessage()); }`"""

    • A. Generic exception
    • B. Caught Exception
    • C. Caught Exception: Generic exception
    • D. Error
  8. 32. What is the output of the following code? `void process() throws IOException { // Code that might throw IOException } public static void main(String[] args) { // How should process() be called? }`

    • A. `process();`
    • B. `try { process(); } catch (IOException e) { ... }`
    • C. `try-catch-finally block is mandatory`
    • D. `if (exceptionPossible) process();`
  9. 33. What is the purpose of the `getMessage()` method of the `Throwable` class?

    • A. To get the name of the exception class
    • B. To get a detailed description of the exception
    • C. To print the stack trace
    • D. To get the cause of the exception
  10. 34. What is the purpose of the `printStackTrace()` method of the `Throwable` class?

    • A. To get the name of the exception class
    • B. To get a detailed description of the exception
    • C. To print the stack trace of the exception
    • D. To get the cause of the exception
  11. 35. What is the purpose of the `getCause()` method of the `Throwable` class?

    • A. To get the name of the exception class
    • B. To get a detailed description of the exception
    • C. To print the stack trace
    • D. To get the underlying cause of the exception
  12. 36. Can you have a `try` block without a `catch` or `finally` block?

    • A. No
    • B. Yes
    • C. Only if it's nested
    • D. Only in specific scenarios