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 3 of 4)

  1. Why might you choose to propagate an exception to a higher layer rather than handle it immediately?Multiple choiceHard
  2. You are designing a method that takes a user ID as input. What should you do if the ID is null?Multiple choiceMedium
  3. In the context of exception handling, what does 'state protection' mean?Multiple choiceMedium
  4. In designing a custom exception, which practice is recommended to provide meaningful diagnostics?Multiple choiceHard
  5. When should you choose to not catch an exception in your code?Multiple choiceMedium
  6. In which scenario does a 'finally' block execute?Multiple choiceEasy
  7. Which statement is true about exception handling in Java?Multiple choiceMedium
  8. Which exception type should you typically throw for invalid method arguments?Multiple choiceEasy
  9. What block should be used to handle exceptions thrown by a try block?Multiple choiceEasy
  10. Why might you choose to extend Exception rather than RuntimeException for a custom exception?Multiple choiceMedium
  11. Why do teams use unchecked exceptions for certain errors in a Java application in production code?Multiple choiceMedium
  12. Which Java keyword is used to define a block of code that can throw an exception?Multiple choiceEasy
  13. Which of the following is a valid reason to catch and rethrow an exception as a different type?Multiple choiceMedium
  14. In Java, what is a recommended practice when logging exceptions for debugging?Multiple choiceMedium
  15. When might a checked exception be justified?Multiple choiceMedium
  16. When should a method catch an exception and take no further action?Multiple choiceMedium
  17. Why is it important to declare checked exceptions in the method signature?Multiple choiceEasy
  18. Which is an anti-pattern in catch blocks?Multiple choiceHard
  19. In a large e-commerce system, why might you create a hierarchy of custom exceptions?Multiple choiceHard
  20. When designing a custom exception, which of the following practices is recommended?Multiple choiceHard
  21. In a try-catch-finally structure, which order do the blocks execute if no exception is thrown?Multiple choiceMedium
  22. In Java, what is a typical mistake developers make when wrapping exceptions?Multiple choiceMedium
  23. Which statement is true about the 'finally' block in exception handling?Multiple choiceMedium
  24. In Java, which practice helps ensure that errors are detected early in the program flow?Multiple choiceEasy
  25. Why is it beneficial to log exceptions with their causes in an e-commerce application?Multiple choiceMedium
  26. In a checkout application, what is the best way to handle a temporary database connection failure during order processing?Multiple choiceHard
  27. In which scenario is it most appropriate to use fail-fast validation in a constructor?Multiple choiceHard
  28. Which Java keyword is used to define a custom exception class?Multiple choiceMedium
  29. In which scenario is it inappropriate to use a custom exception?Multiple choiceMedium
  30. How would you explain the key difference between a checked exception and an unchecked exception in an interview?Multiple choiceEasy
  31. How should a method handle exceptions internally if it decides not to propagate them?Multiple choiceHard
  32. Which statement best explains why unchecked exceptions exist?Multiple choiceMedium
  33. What happens if a method throws a checked exception that is not handled or declared?Multiple choiceEasy
  34. A repository catches SQLException. What is the strongest reason to do that?Multiple choiceEasy
  35. Which principle is important when deciding between a checked and unchecked exception for a custom exception?Multiple choiceHard
  36. Which exception type should be used to indicate a logic error?Multiple choiceEasy
  37. How would you explain a key difference between checked and unchecked exceptions in a Java interview?Multiple choiceHard
  38. What keyword is used to manually throw an exception in Java?Multiple choiceEasy
  39. How would you explain an anti-pattern in error handling when dealing with Java exceptions in an interview?Multiple choiceHard
  40. Why validate an empty cart or invalid quantity before contacting inventory or payment systems?Multiple choiceMedium
  41. What should a method that declares a checked exception be responsible for?Multiple choiceEasy
  42. In a try-catch-finally block, if an exception is thrown in the catch block, what is executed?Multiple choiceHard
  43. Why does rejecting invalid input early in a method matter in practice?Multiple choiceMedium
  44. Why does a 'try' block matter in practice?Multiple choiceMedium
  45. Why does a catch block in Java matter in practice?Multiple choiceEasy
  46. In an e-commerce checkout system, which exception type should you use for handling invalid payment details?Multiple choiceMedium
  47. Why is it important to include context in error messages?Multiple choiceMedium
  48. Which design consideration is crucial when creating a custom exception hierarchy?Multiple choiceHard
  49. Why should you create a custom exception in Java?Multiple choiceEasy
  50. Why is it important to use fail-fast validation in a constructor?Multiple choiceEasy