Given1. class Test{2. public static void main(String args[]){3. try{4. new Test().meth();5. }catch(ArithmeticException e){6. System.out.print("Arithmetic");7. }finally{8. System.out.print("final 1");9. }catch(Exception e){10. System.out.print("Exception");11. }finally{12. System.out.print("final 2");13. }14. }15. 16. public void meth()throws ArithmeticException{17. for(int x=0;x<5;x++){18. int y = (int)5/x;19. System.out.print(x);20. }21. }22. }What is the output?*Arithmetic final 2Arithmetic final 1ArithmeticCompilation fails.Exception final 2
Question
Given1. class Test{2. public static void main(String args[]){3. try{4. new Test().meth();5. }catch(ArithmeticException e){6. System.out.print("Arithmetic");7. }finally{8. System.out.print("final 1");9. }catch(Exception e){10. System.out.print("Exception");11. }finally{12. System.out.print("final 2");13. }14. }15. 16. public void meth()throws ArithmeticException{17. for(int x=0;x<5;x++){18. int y = (int)5/x;19. System.out.print(x);20. }21. }22. }What is the output?*Arithmetic final 2Arithmetic final 1ArithmeticCompilation fails.Exception final 2
Solution
The output will be "Compilation fails".
This is because in Java, the 'finally' block is always associated with a 'try' block. It is used to put important code such as clean-up code, etc. It cannot exist without a 'try' block.
In the given code, there are two 'finally' blocks but only one 'try' block. The second 'finally' block does not have a corresponding 'try' block, hence the code will fail to compile.
Similar Questions
the correct answerWhat will be the output of the following Java program? class CT { public static void main(String args[]) { try { int x, y; y = 0; x = 5 / y; System.out.print("A"); } catch(ArithmeticException e) { System.out.print("B"); } } }
public class Test { String msg = null; public void show(){ try { try { System.out.println(10/0); } catch(ArithmeticException e) { msg = e.getMessage(); //Line 1 } try { System.out.println("IITM".charAt(5)); } catch(StringIndexOutOfBoundsException e) { msg = e.getMessage(); //Line 2 } } finally { System.out.println(msg); } } public static void main(String[] args) { Test obj = new Test(); obj.show(); } }
rrect answerWhat will be the output of the following Java code? class Name { public static void main(String args[]) { try { int ct1 = 0; int ct2 = 5; int ct3 = ct2 / ct1; System.out.print("Hello"); } catch(Exception e) { System.out.print("CodeTantra"); } } }
What will be the output of the program?public class X { public static void main(String [] args) { try { badMethod(); /* Line 7 */ System.out.print("A"); } catch (Exception ex) /* Line 10 */ { System.out.print("B"); /* Line 12 */ } finally /* Line 14 */ { System.out.print("C"); /* Line 16 */ } System.out.print("D"); /* Line 18 */ } public static void badMethod() { throw new RuntimeException(); } }ABBCABCBCD
What will be the output of the following Java program?class exception_handling { public static void main(String args[]) { try { System.out.print("Hello" + " " + 1 / 0); } catch(ArithmeticException e) { System.out.print("World"); } } }a) Hellob) Worldc) HelloWorldd) Hello World
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.