Skip to content
Sharp

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 (RuntimeException and subclasses) needn't be.
  • try-with-resources closes resources automatically, in reverse order of declaration; exceptions from close() are attached as *suppressed*.
  • finally runs on normal exit, return or exception, but not after System.exit().

Questions (page 1 of 4)

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