Multiple choice technology programming languages

class Demo { static String s = "-"; public static void main(String[] args) { new Demo().method1() ; System.out.println(s); } void method1() { try { method2();} catch (Exception e) { s += "c"; } } void method2() throws Exception { method3(); s += "2"; method3(); s += "2b"; } void method3() throws Exception { throw new Exception(); } } What is the result?

  1. -

  2. -c

  3. -c2

  4. -2c

  5. -c22b

  6. Compilation fails.

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

method1 calls method2, which calls method3 (first call). method3 throws Exception immediately. This exception propagates to method1's catch block, appending 'c' to s. The code 's += 2' and everything after in method2 never executes (unreachable after exception). The initial s='-' becomes '-c'.