Question 39Asked in DSSSB TGT Computer Science (Technical), 13 Sep 2025, Shift 3 (5:00 PM)

What will be the output of the following code? public class ExceptionDemo {

What will be the output of the following code? public class ExceptionDemo { public static void main(String[] args) { try { int[] numbers = {1, 2, 3}; System.out.println(numbers[5]); } catch (Exception e) { System.out.println("Exception caught: " + e.getClass().getName()); } finally { System.out.println("Execution completed."); } } }
  1. AException caught: java.lang.Exception . Execution completed.
  2. BException caught: java.lang.ArrayIndexOutOfBoundsException. Execution completed.
  3. CCompilation error
  4. DException caught: java.lang.IndexOutOfBoundsException . Execution completed.
Show answerHide answer

Correct answer: (B) Exception caught: java.lang.ArrayIndexOutOfBoundsException. Execution completed.

Explanation

numbers[5] is past the end of the array, so an ArrayIndexOutOfBoundsException is thrown. getClass().getName() gives its full name, and finally always runs.

From DSSSB TGT Computer Science PYQ (Technical), 13 Sep 2025, Shift 3 (5:00 PM)

This is question 39 of DSSSB TGT Computer Science PYQ (Technical), 13 Sep 2025, Shift 3 (5:00 PM). See all questions of this paper with answers, or attempt the paper as a timed online test to get your score and all-India rank.