ProgrammingHardHCL · Tech Mahindra pattern

Does this compile?

Does this compile?
class Demo {
    void risky() { throw new IllegalArgumentException(); }
    void call() {
        risky();
    }
}
  1. A. No: risky() must declare throws IllegalArgumentException
  2. B. Yes: IllegalArgumentException is unchecked (a RuntimeException), so it need not be declared or caught
  3. C. Yes, but only because call() is void
  4. D. No: unchecked exceptions must still be caught somewhere

Correct answer: B. Yes: IllegalArgumentException is unchecked (a RuntimeException), so it need not be declared or caught

IllegalArgumentException extends RuntimeException, making it unchecked. Unchecked exceptions are exempt from Java's compile-time catch-or-declare requirement, so this compiles even though neither method mentions it.

More OOP with C++ and Java questions

Exams that ask this topic