Exceptions interview questions
Checked vs unchecked, try-with-resources, finally.
160 questions with answers and explanations · 158 flashcards
Key ideas
- Checked exceptions (
IOException) must be caught or declared; unchecked (RuntimeExceptionand subclasses) needn't be. - try-with-resources closes resources automatically, in reverse order of declaration; exceptions from
close()are attached as *suppressed*. finallyruns on normal exit, return or exception, but not afterSystem.exit().
Questions (page 4 of 4)
- When wrapping exceptions, which practice helps ensure observability?Multiple choiceMedium
- Why does Java use exceptions instead of error codes?Multiple choiceEasy
- Which design principle is violated if exceptions are used to handle expected conditions?Multiple choiceHard
- Which approach is the safest default for handling exceptions in library code?Multiple choiceMedium
- How can you ensure resources are released after usage?Multiple choiceEasy
- Why does throwing an IllegalArgumentException in a method help in practice?Multiple choiceMedium
- In a complex e-commerce system, some errors should be logged and ignored. Which exception type suits this pattern?Multiple choiceHard
- When is an unchecked exception often the clearer choice?Multiple choiceMedium
- Why do teams use checked exceptions for API design in production code?Multiple choiceMedium
- Which Java feature allows you to handle multiple exception types in a single catch block?Multiple choiceMedium