Question 35Asked in DSSSB TGT Computer Science (Technical), 12 Sep 2025, Shift 1 (9:00 AM)

public class ExceptionTest { public static void main(String[] args) {

public class ExceptionTest { public static void main(String[] args) { try { System.out.println("Result: " + calculate(10, 0)); } catch (ArithmeticException e) { System.out.println("ArithmeticException caught: " + e.getMessage()); } catch (Exception e) { System.out.println("General exception caught: " + e.getMessage()); } finally { System.out.println("In finally block."); } } public static int calculate(int a, int b) throws Exception { try { return a / b; } catch (ArithmeticException e) { throw new Exception("Division by zero not allowed"); } finally { System.out.println("Inner finally block executed."); } } } What will be the output of the program?
  1. AInner finally block executed. General exception caught: / by zero In finally block.
  2. BInner finally block executed. General exception caught: Division by zero not allowed In finally block.
  3. CInner finally block executed. Result: 0 In finally block.
  4. DInner finally block executed. ArithmeticException caught: / by zero In finally block.
Show answerHide answer

Correct answer: (B) Inner finally block executed. General exception caught: Division by zero not allowed In finally block.

Explanation

10/0 throws ArithmeticException inside calculate, which is caught and replaced by new Exception("Division by zero not allowed"). The inner finally prints first, then main's catch (Exception) prints that message, and finally main's finally block runs.

From DSSSB TGT Computer Science PYQ (Technical), 12 Sep 2025, Shift 1 (9:00 AM)

This is question 35 of DSSSB TGT Computer Science PYQ (Technical), 12 Sep 2025, Shift 1 (9:00 AM). 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.