Select the correct answerWhat will be the output of the following Java code? class Codetantra { public static void main(String args[]) { try { System.out.print("Good" + " " + 1 / 0); } finally { System.out.print("Morning"); } } }OptionsGoodCompilation ErrorFirst "Morning" then "Exception"Morning
Question
Select the correct answerWhat will be the output of the following Java code? class Codetantra { public static void main(String args[]) { try { System.out.print("Good" + " " + 1 / 0); } finally { System.out.print("Morning"); } } }OptionsGoodCompilation ErrorFirst "Morning" then "Exception"Morning
Solution
The correct answer is "First 'Morning' then 'Exception'".
Here's the step-by-step explanation:
- The Java code starts executing from the main method.
- It enters the try block and starts executing the statements inside it.
- The first statement inside the try block is a print statement which prints "Good" and then tries to perform the operation 1/0.
- Since division by zero is undefined, Java throws an ArithmeticException.
- Before the exception is thrown to the calling method (which in this case would terminate the program because it's not caught), the finally block is executed.
- The finally block has a print statement which prints "Morning".
- After the finally block is executed, the exception is thrown to the calling method, which terminates the program.
So, the output will be "Morning" first, and then the program will terminate with an ArithmeticException.
Similar Questions
Select the correct 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"); } } }OptionsCodeTantraHelloHelloCodeTantraCompilation Error
rect answerWhat will be the output of the following Java code? class Codetantra { public static void main(String args[]) { try { System.out.print("Good" + " " + 1 / 0); } finally { System.out.print("Morning"); } } }
Select the correct answerWhat will be the output of the following Java program? class CT { public static void main(String args[]) { try { System.out.print("Codetantra" + " " + 1 / 0); } catch(ArithmeticException e) { System.out.print("World"); } } }OptionsCodetantraCodetantraWorldCodetantra WorldWorld
Select 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"); } } }OptionsBACompilation ErrorRuntime Error
Select the correct answerWhat will be the output of the following Java code? class Codetantra { final public int output(int x, int y) { return 1; } } class Program extends Codetantra { public int output(int x, int y) { return 2; } } public class result { public static void main(String args[]) { Program object = new Program(); System.out.print("y is " + y.output(0, 1)); } }Optionsy is : 0y is : 1Compilation ErrorAn exception is thrown at runtime
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.