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); } }Options050Compilation ErrorRuntime Error
Question
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); } }Options050Compilation ErrorRuntime Error
Solution
The correct answer is "0".
Here's the step-by-step explanation:
-
The program starts with the declaration of the class CT and the main method.
-
Inside the main method, a try-catch block is initiated. This is used to handle exceptions that might occur during the execution of the program.
-
Inside the try block, two integer variables, 'a' and 'sum', are declared. 'sum' is initialized with the value 10.
-
A for loop is started with 'a' initialized to -1 and the condition 'a < 3'. This means the loop will run for 'a' values -1, 0, 1, and 2.
-
Inside the loop, 'sum' is divided by 'a' and the result is assigned back to 'sum'.
-
When 'a' is -1, the division is valid and 'sum' becomes -10.
-
However, when 'a' is 0, an ArithmeticException is thrown because division by zero is undefined in mathematics.
-
The catch block catches this exception and the program prints "0".
-
After the catch block, the program tries to print 'sum'. However, because an exception was thrown, the execution of the try block was stopped and control was transferred to the catch block. Therefore, 'sum' is not printed and the program ends after printing "0".
So, the output of the program is "0".
Similar Questions
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"); } } }Options0-1-10-101
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
What will be the output of the following Java program?class control_statements { public static void main(String args[]) { int sum = 0; for (int u = 5, v = 5; u < 10 & v < 10; ++u, v = u + 1) sum += u; System.out.println(sum); } }Options261015compilation error
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"); }}Options“Programming In Java”Compile time errorArithmeticExceptionRun time error
Problem statementSend feedbackWhat is the output of the following code?public class Solution { public int getSum(int a, int b) { int sum = a + b; } public static void main(String args[]) { Solution obj = new Solution(); System.out.println(obj.getSum(10, 20)); } }Options: Pick one correct answer from below30Runtime errorCompilation errorNone of the above
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.