Select the correct answerWhat will be the output of the following Java code? class CodeTantra { public static void main(String args[]) { try { int x, sum; sum = 7; for (x = -1; x < 4; ++x) { sum = (sum / x); System.out.print(x); } } catch(ArithmeticException e) { System.out.print("0"); } } }Options-1-10-1010
Question
Select the correct answerWhat will be the output of the following Java code? class CodeTantra { public static void main(String args[]) { try { int x, sum; sum = 7; for (x = -1; x < 4; ++x) { sum = (sum / x); System.out.print(x); } } catch(ArithmeticException e) { System.out.print("0"); } } }Options-1-10-1010
Solution
The correct answer is "0". The reason is that in the first iteration of the loop, x is -1, so the operation sum / x is valid. However, in the next iteration, x is incremented to 0, and the operation sum / x throws an ArithmeticException because division by zero is undefined. This exception is caught by the catch block, and "0" is printed. The loop then stops executing because an unhandled exception has occurred.
Similar Questions
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 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 final value of sum?int sum = 0;for (int i = 1; i <= 5; i++) { try { if (i == 3) { throw new Exception(); } sum += i; } catch (Exception e) { continue; }}System.out.println(sum);Options13121015
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"); } } }OptionsACompilation ErrorRuntime ErrorB
Select the correct answerWhat will be the output of the following Java program, if we run as “java Codetantra 4 7 9”? class Codetantra { public static void main(String [] args) { String [][] argument = new String[2][2]; int a; argument[0] = args; a = argument[0].length; for (int b = 0; b < a; b++) System.out.print(" " + argument[0][b]); } }Options4 04 44 0 94 7 9
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.