Bird
0
0

What will be the output of this C# code?

medium📝 Predict Output Q13 of 15
C Sharp (C#) - Exception Handling
What will be the output of this C# code?
try {
  int x = 10 / 0;
  Console.WriteLine("Result: " + x);
} catch (DivideByZeroException) {
  Console.WriteLine("Cannot divide by zero.");
}
AResult: 0
BNo output
CCannot divide by zero.
DRuntime error and program crashes
Step-by-Step Solution
Solution:
  1. Step 1: Identify the error in the try block

    The code tries to divide 10 by 0, which causes a DivideByZeroException.
  2. Step 2: Check the catch block handling

    The catch block catches DivideByZeroException and prints "Cannot divide by zero." instead of crashing.
  3. Final Answer:

    Cannot divide by zero. -> Option C
  4. Quick Check:

    Divide by zero caught = D [OK]
Quick Trick: Divide by zero triggers catch block output [OK]
Common Mistakes:
MISTAKES
  • Expecting program to crash instead of catching error
  • Thinking output is 'Result: 0'
  • Ignoring the catch block

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Sharp (C#) Quizzes