ProgrammingHardTCS NQT · Infosys pattern

What is printed?

What is printed?
class MyEx extends RuntimeException {
    public String getMessage() { return "custom:" + super.getMessage(); }
}
public static void main(String[] a) {
    try {
        throw new MyEx();
    } catch (MyEx e) {
        System.out.print(e.getMessage());
    }
}
  1. A. custom:null
  2. B. null
  3. C. custom:
  4. D. Compilation error: getMessage() cannot be overridden

Correct answer: A. custom:null

new MyEx() calls the no-arg RuntimeException constructor, which stores a null message. super.getMessage() returns that null, and string concatenation renders it as the text "null", giving "custom:null".

More OOP with C++ and Java questions

Exams that ask this topic