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 1 of 4)
- Which of these is an unchecked exception?Multiple choiceEasy
- R prints "open X" in its constructor and "close X" in close(). Put the printed lines in order.OrderingHard
- Which statements about finally are true? Select all that apply.Select all that applyMedium
- Explain checked vs unchecked exceptions in Java. When would you use each in your own API?Interview answerMedium
- Why does chaining exceptions in Java help in practice?Multiple choiceEasy
- Why can overusing exception wrapping become a problem in practice?Multiple choiceHard
- Why wrap an exception with the original cause instead of replacing it completely?Multiple choiceHard
- Which of the following is NOT a recommended practice for exception handling?Multiple choiceEasy
- Which method is used to retrieve the original cause of a chained exception?Multiple choiceEasy
- What exception type should you throw when encountering a database connection error in an e-commerce application?Multiple choiceEasy
- How would you explain a key reason to use exception wrapping in a Java interview?Multiple choiceEasy
- What should you do to ensure a custom exception class is serializable?Multiple choiceEasy
- How would you explain a key benefit of using custom exceptions in a Java application in an interview?Multiple choiceMedium
- When designing a Java API, how should exceptions be documented?Multiple choiceMedium
- Why can catching the Exception class in Java become a real problem?Multiple choiceMedium
- How can fail-fast validation impact the design of a Java class intended for secure financial transactions?Multiple choiceHard
- Why can overusing checked exceptions in a library become a problem in production code?Multiple choiceHard
- In which scenario does exception wrapping make code more reliable?Multiple choiceMedium
- What role does the 'finally' block serve in Java exception handling?Multiple choiceEasy
- In a multithreaded e-commerce application, how should you handle exceptions to ensure thread safety?Multiple choiceHard
- Which of the following blocks is always executed in a try-catch structure?Multiple choiceEasy
- How should exceptions be handled in a payment processing API to ensure reliability?Multiple choiceHard
- Which statement correctly distinguishes throw from throws?Multiple choiceHard
- What happens if both the 'try' and 'finally' blocks contain return statements?Multiple choiceHard
- In which scenario should you convert a checked exception to an unchecked one?Multiple choiceHard
- Consider the following code snippet. What is the primary issue with the exception handling?Multiple choiceHard
- In a payment processing API, how should exceptions be propagated?Multiple choiceHard
- Which of the following is a valid reason to extend Exception rather than RuntimeException for a custom exception?Multiple choiceMedium
- In an exception handling block, why is it important to provide useful context in the error messages?Multiple choiceHard
- Consider the following code snippet. What will happen if the input is null?Multiple choiceMedium
- How would you explain a likely consequence of not using fail-fast validation in an interview?Multiple choiceMedium
- In a Java method, what is the consequence of catching an exception and not handling it properly?Multiple choiceMedium
- Given the following code snippet, what is the main issue with the exception handling approach?Multiple choiceHard
- Why is using an empty catch block risky in production code?Multiple choiceHard
- In Java, which method is commonly overridden in a custom exception to provide additional information?Multiple choiceMedium
- How would you explain a potential issue with catching and ignoring exceptions silently in an interview?Multiple choiceMedium
- How would you explain a common mistake when wrapping exceptions in an interview?Multiple choiceMedium
- Why do teams use checked exceptions in production code?Multiple choiceEasy
- Why does the difference between checked and unchecked exceptions in Java matter in practice?Multiple choiceEasy
- Which of the following is NOT a recommended practice when dealing with exceptions in a Java application?Multiple choiceHard
- In a checkout system, when should you use an unchecked exception?Multiple choiceMedium
- Given the code snippet, which exception handling strategy is demonstrated?Multiple choiceHard
- Why is it beneficial to use immutable objects in fail-fast validation?Multiple choiceHard
- Which exception type should you use for an invalid user input in a checkout process?Multiple choiceMedium
- Why does a finally block in Java exception handling matter in practice?Multiple choiceEasy
- Consider the following Java method signature. What should you do if 'amount' is negative? public void processPayment(double amount)?Multiple choiceMedium
- Which approach is the safest default when handling exceptions?Multiple choiceMedium
- Why do teams use exceptions in Java in production code?Multiple choiceEasy
- Which exception is commonly used in Java for indicating that an argument passed to a method is inappropriate?Multiple choiceEasy
- In Java, if a method does not handle a checked exception, what must it do?Multiple choiceHard