Bird
0
0

What will be the output of this code?

hard📝 Application Q9 of 15
Java - Exception Handling

What will be the output of this code?

public class Example {
    public static void main(String[] args) {
        try {
            System.out.print("A");
            throw new RuntimeException();
        } catch (Exception e) {
            System.out.print("B");
            return;
        } finally {
            System.out.print("C");
        }
        System.out.print("D");
    }
}
AABCD
BAC
CABC
DBCD
Step-by-Step Solution
Solution:
  1. Step 1: Trace try block

    Prints "A" then throws RuntimeException caught by catch.
  2. Step 2: Execute catch block

    Catch prints "B" and hits return, but finally still runs.
  3. Step 3: Execute finally block

    Finally prints "C" before method returns; "D" is never printed.
  4. Final Answer:

    ABC -> Option C
  5. Quick Check:

    finally runs even after return in catch [OK]
Quick Trick: finally runs even if catch returns [OK]
Common Mistakes:
  • Expecting 'D' to print after return
  • Ignoring finally block output
  • Assuming return skips finally

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes