Exception Handling – Multiple Choice Questions (MCQs)
-
-
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\"); }`"""
-
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\"); }`"""
-
28. What is the output of the following code? `try { System.out.println(\Try block\"); } finally { System.out.println(\""Finally block\""); }`"""
-
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\""); }`"""
-
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\""); }`"""
-
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()); }`"""
-
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? }`
-
33. What is the purpose of the `getMessage()` method of the `Throwable` class?
-
34. What is the purpose of the `printStackTrace()` method of the `Throwable` class?
-
35. What is the purpose of the `getCause()` method of the `Throwable` class?
-
36. Can you have a `try` block without a `catch` or `finally` block?