Bird
0
0

What will be the output of the following Java code?

medium📝 Predict Output Q4 of 15
Java - Exception Handling
What will be the output of the following Java code?
public class Example {
  public static void main(String[] args) {
    try {
      int value = 100 / 0;
      System.out.println(value);
    } catch (ArithmeticException e) {
      System.out.println("Division by zero error");
    }
  }
}
ACompilation error
B100
C0
DDivision by zero error
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the try block

    The code attempts to divide 100 by 0, which causes an ArithmeticException.
  2. Step 2: Catch block execution

    The catch block catches the ArithmeticException and prints "Division by zero error".
  3. Final Answer:

    Division by zero error -> Option D
  4. Quick Check:

    Division by zero triggers ArithmeticException [OK]
Quick Trick: Division by zero triggers ArithmeticException catch [OK]
Common Mistakes:
  • Expecting the program to print 100
  • Assuming division by zero returns 0
  • Thinking this causes a compile-time error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes