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"); } finally { System.out.print("C"); } } }OptionsAACBCB
Question
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"); } finally { System.out.print("C"); } } }OptionsAACBCB
Solution
The correct answer is B. The program will output "BC".
Here's the step by step explanation:
-
The program starts with defining two integer variables X and Y. Y is set to 0.
-
Then, the program tries to divide 5 by Y (which is 0). In Java, division by zero results in an ArithmeticException.
-
The catch block catches this exception and the program prints "B".
-
The finally block is always executed whether an exception is thrown or not. So, it prints "C".
-
Therefore, the output of the program is "BC".
Similar Questions
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 is the output of the following program?public class Nptel{ public static void main(String[] args) { try { int a = 5 / 0; } catch (Exception e) { catch (ArithmeticException a) { } } System.out.println("Programming In Java"); }}OptionsRun time error“Programming In Java”Compile time errorArithmeticException
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"); } } }
Select the correct answerWhat will be the output of the following Java program? class CT { public static void main(String args[]) { try { int a, sum; sum = 10; for (a = -1; a < 3; ++a) sum = (sum / a); } catch(ArithmeticException e) { System.out.print("0"); } System.out.print(sum); } }Options0Compilation ErrorRuntime Error05
Select the correct answerWhat 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"); } } }OptionsHelloWorldHelloWorldHello 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.