Knowee
Questions
Features
Study Tools

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

Question

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

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

The output of the program will be "BCD". Here's why:

  1. The main method is called, which in turn calls badMethod().
  2. badMethod() throws a RuntimeException.
  3. Since the RuntimeException is not caught (the catch block is looking for an Exception, not a RuntimeException), the program jumps to the finally block.
  4. The finally block prints "C".
  5. After the finally block, the program continues with the next line of code after the try-catch-finally, which prints "D".

So, the output is "BCD".

This problem has been solved

Similar Questions

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

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");         }      } }

Consider following code segment1. import java.util.*;2. public class Program{3. public static void main(String[] args) {4. int []a = {3,2,1,0};5. int y =  a.length;6. while(y>=0){7.   System.out.print(a[--y]);8. } 9. }10. }  What would be the output, if it is executed as a program?*Never ending loop after printing 32100123 followed by an exception.01233210Compilation fails

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");    }    finally {     System.out.print("C");    }  } }

Consider a Program:public class Test {            public static void main(String[] args) {             int[] numbers = {1, 2, 3, 4, 5};             for (int i = 0; i <= numbers.length; i++) {             System.out.println(numbers[i]);             }            }}Which of the following is an output of the above program ? a. It will print numbers from 1 to 5 b. It will print numbers from 1 to 5 and then throw an exception c. It will compile but not run d. It will not compile

1/3

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.