🧰 Exceptions, I/O and Reflection — interview questions
12 questions. Filter by what the role asks for; open one for the six layers of the answer. The course is where the answers come from.
12 of 12
01A teammate declares `throws IOException` on a method three layers above where the exception is actually thrown, and every intermediate caller does the same because "that's what the compiler wants." What's actually wrong with reflexively propagating checked exceptions, and what rule should decide checked versus unchecked instead?Exceptionseasy0-2y02A method opens a database connection in a `try-with-resources` block. The body throws a business exception, and `close()` on the connection also throws (the pool rejects a broken connection). What does the caller actually catch, and where did the `close()` failure go?Exceptionseasy0-2y03A service throws a single `ServiceException` for everything — not found, validation failure, and a downstream timeout all end up as the same type with different messages. What's wrong with that design, and how would you restructure the hierarchy?Exception designmedium0-2y04A colleague shows you code that throws a checked `IOException` straight out of a `Runnable.run()` — no wrapping, no `UncheckedIOException` — and it compiles cleanly with no error anywhere. How is that possible, and what does it prove about what "checked" actually means?Exceptionshard5-8y05A file-deletion call using `java.io.File` returns `false`, and the log just says "could not delete." A teammate wants to know why the delete failed — permission denied, the file was already gone, or something else. Why can't `File` tell you, and what changes with `java.nio.file.Files`?Files and I/Omedium0-2y06An upload endpoint calls `Files.readAllBytes(uploadedFile)` to validate the content before storing it. It works fine in every test, then one production upload of a large file takes down the instance with an `OutOfMemoryError`. What's the actual failure mode, and what should the endpoint do instead?Files and I/Oeasy0-2y07A service processes very large log files and switches from `Files.lines()` to `FileChannel.map()` for a performance win. What is `MappedByteBuffer` actually doing differently from a normal read, and what's the operational catch nobody mentioned in the pull request?Files and I/Ohard5-8y08An immutable request record adds a new field in the middle of its constructor parameter list to match updated API documentation, with each parameter still carrying its own `@JsonProperty`. Does that reordering risk Jackson binding an existing client's JSON values to the wrong parameters?JSON and Jacksonmedium3-5y09A payments service needs to deserialize a polymorphic `PaymentMethod` — `CardPayment`, `BankTransferPayment`, `WalletPayment` — from a JSON field that names the subtype. A junior engineer suggests enabling `ObjectMapper.enableDefaultTyping()` so any request body can just work without per-type annotations. Why is that a serious security decision disguised as a convenience one, and what should be done instead?JSON and Jacksonsenior8+y10A team writes their own annotation, forgets `@Retention(RetentionPolicy.RUNTIME)`, and the reflection-based code that's supposed to act on it never fires — no exception, no warning, the annotated behavior just silently never triggers. Where, precisely, did the annotation actually go?Annotations and reflectionmedium3-5y11A `@Transactional` method on an `OrderService` bean calls another `@Transactional` method on the same class via `this.otherMethod()`. In production, a failure partway through leaves the database in a partially-committed state that the second method's transactionality was supposed to prevent. What actually happened, and why doesn't `@Transactional` protect this call?Annotations and reflectionhard5-8y12A high-throughput lookup service throws `NotFoundException` for an ordinary "no such record" outcome, thousands of times a second under normal load. Profiling shows a meaningful chunk of CPU going into exception construction, not into the throw itself. What's actually expensive here, and how would you fix it without changing the exception's meaning?Exception designsenior8+y