When using try-with-resources, which interface must the resource implement?
AutoCloseable (or its subinterface Closeable) is required for try-with-resources.
Why this answer
The try-with-resources statement in Java requires that any resource declared in its parentheses implements the `AutoCloseable` interface (or its subinterface `Closeable`). This interface defines a single `close()` method, which the JVM automatically invokes at the end of the try block, ensuring proper resource management without needing an explicit `finally` block.
Exam trap
Oracle often tests the misconception that any interface with a single method (like `Runnable`) qualifies for try-with-resources, but the key requirement is that the interface must extend `AutoCloseable` and its `close()` method must be the one invoked for cleanup.
How to eliminate wrong answers
Option A is wrong because `Serializable` is a marker interface used for serializing object state to a byte stream, not for resource management. Option B is wrong because `Runnable` is a functional interface designed for thread execution via its `run()` method, not for closing resources. Option D is wrong because `Comparable` is used to define a natural ordering of objects via `compareTo()`, and has no connection to resource cleanup or the try-with-resources mechanism.