Knowee
Questions
Features
Study Tools

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

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);  } }Options0Compilation ErrorRuntime Error05

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

The correct answer is "0".

Here's the step-by-step explanation:

  1. The program starts with the declaration of the class CT and the main method.

  2. Inside the main method, a try-catch block is used to handle any potential ArithmeticException that might occur.

  3. The variables 'a' and 'sum' are declared. 'sum' is initialized with the value 10.

  4. A for loop is used to iterate from 'a' = -1 to 'a' < 3.

  5. Inside the loop, 'sum' is divided by 'a'.

  6. In the first iteration, 'a' is -1, so 'sum' becomes -10.

  7. In the second iteration, 'a' is 0, which leads to an ArithmeticException because division by zero is undefined in mathematics.

  8. The catch block catches this exception and prints "0".

  9. After the catch block, the program tries to print 'sum'. However, because the exception was thrown, the program control was transferred to the catch block, and the line of code after the division operation inside the try block was not executed.

  10. Therefore, only "0" is printed, and not the value of 'sum'.

So, the output of the program is "0".

This problem has been solved

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

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

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

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

1/3

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.