219k views
1 vote
What is displayed on the console when running the following program? class Test { public static void main(String[] args) { try { method(); System.out.println("After the method call"); } catch (RuntimeException ex) { System.out.println("RuntimeException"); } catch (Exception ex) { System.out.println("Exception"); } } static void method() throws Exception { try { String s = "5.6"; Integer.parseInt(s); // Cause a NumberFormatException int i = 0; int y = 2 / i; System.out.println("Welcome to Java"); } catch (RuntimeException ex) { System.out.println("RuntimeException"); } catch (Exception ex) { System.out.println("Exception"); } } } Question 8 options:

User CXL
by
4.3k points

1 Answer

2 votes

Answer:

RuntimeException

After the method call

Step-by-step explanation:

Image showing the program output when executed is attached.

In the code snippet given, inside the main method which is the beginning of program execution, there is a try-catch blocks.

The try block is executed first and the catch block is only executed if an exception is thrown.

The try block has two statement to execute:

try {

method();

System.out.println("After the method call");

}

The first statement is executed by calling the method called method(). The second statement display the output: "After the method call".

When method() is called, a runtime exception is thrown and a catch block is executed which display the message: "RuntimeException".

runtime exception occur during the program execution. From the above code snippet, the runtime exception is thrown/caused by the line below:

Integer.parseInt(s);

It is cause because the when the string is converted to integer, it is not assigned to any variable and this lead to the runtime exception.

What is displayed on the console when running the following program? class Test { public-example-1
User Jim C
by
4.9k points