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

  1. Which Java keyword would you use to handle exceptions in Java?Multiple choiceEasy
  2. Which of the following best describes a checked exception in Java?Multiple choiceEasy
  3. Inventory is reserved successfully, then payment fails. What is the strongest workflow design?Multiple choiceMedium
  4. Why is it useful to creating a custom exception in Java?Multiple choiceEasy
  5. Why does stack unwinding during exception handling change runtime behavior?Multiple choiceMedium
  6. When should a custom exception extend RuntimeException?Multiple choiceMedium
  7. Why is it important to avoid catching Throwable in Java applications?Multiple choiceMedium
  8. Given the following code, what will be the output? try { System.out.println("Try"); return; } finally { System.out.println("Finally"); }?Multiple choiceMedium
  9. Which is a poor practice when dealing with exceptions in a checkout flow?Multiple choiceHard
  10. In Java, what is the result of not handling a checked exception?Multiple choiceEasy
  11. How would you explain a common reason to use a 'finally' block in an interview?Multiple choiceEasy
  12. Why might you choose to throw an unchecked exception instead of a checked exception?Multiple choiceEasy
  13. Which method is used to retrieve the original exception cause when an exception is wrapped?Multiple choiceMedium
  14. How can you ensure that resources are properly closed in Java when exceptions are thrown?Multiple choiceHard
  15. What does the 'throws' keyword in Java indicate?Multiple choiceMedium
  16. How would you explain a suitable approach for handling exceptions in a high-reliability checkout flow in an interview?Multiple choiceHard
  17. Which of the following is a valid reason to throw a custom exception during the checkout process of an e-commerce application?Multiple choiceMedium
  18. Which problem arises when a catch block catches exceptions generally using 'Exception e'?Multiple choiceMedium
  19. Why does the 'throws' keyword in a method declaration matter in practice?Multiple choiceEasy
  20. In an e-commerce checkout flow, why is catching only specific exceptions considered a best practice?Multiple choiceHard
  21. Why do teams use custom exceptions in Java in production code?Multiple choiceMedium
  22. In a reliable checkout flow, where should you handle exceptions that might indicate a system failure?Multiple choiceMedium
  23. Which principle should guide the naming of custom exceptions?Multiple choiceHard
  24. Which scenario justifies converting a checked exception to an unchecked exception?Multiple choiceMedium
  25. Why is it important to document unchecked exceptions in method documentation?Multiple choiceEasy
  26. In the context of exception handling, what is exception propagation?Multiple choiceMedium
  27. In a multi-layered Java application, how can exception translation improve reliability?Multiple choiceHard
  28. Consider the following method signature: public void processOrder() throws IOException. What does this indicate?Multiple choiceMedium
  29. Why should exceptions not be used for normal control flow?Multiple choiceMedium
  30. What happens when a runtime exception is thrown but not caught within a method?Multiple choiceMedium
  31. Which of the following is a good practice when wrapping exceptions?Multiple choiceMedium
  32. Why is fail-fast validation particularly important in multi-threaded environments?Multiple choiceMedium
  33. Which Java exception type should be used for an illegal argument passed to a method?Multiple choiceEasy
  34. When designing an API, why is it important to map exceptions to higher layers?Multiple choiceMedium
  35. What happens if an exception is not caught in a try-catch-finally block?Multiple choiceEasy
  36. Why do teams use checked exceptions in Java in production code?Multiple choiceEasy
  37. Which of the following will execute if a 'try' block completes without error, but the 'finally' block throws an exception?Multiple choiceHard
  38. Why can catching exceptions too broadly (e.g., catching Exception) become a problem in production code?Multiple choiceMedium
  39. Why should you avoid using exceptions for control flow in Java?Multiple choiceMedium
  40. Why might using multiple catch blocks be beneficial?Multiple choiceMedium
  41. What happens if an exception is thrown in the 'finally' block?Multiple choiceMedium
  42. What type of exception does the following code throw if the divisor is zero?Multiple choiceEasy
  43. How can you ensure reliability in a Java application when dealing with external resources?Multiple choiceMedium
  44. In the context of a robust checkout flow, what is the primary reason to throw custom exceptions?Multiple choiceHard
  45. Why does a 'finally' block in Java exception handling matter in practice?Multiple choiceEasy
  46. How does preserving the cause of an exception help in a checkout flow?Multiple choiceHard
  47. Why can converting checked exceptions to unchecked exceptions become a problem in production code?Multiple choiceHard
  48. In Java, what happens if a method throws a checked exception but does not declare it in the method signature?Multiple choiceEasy
  49. Why does a 'catch' block in exception handling matter in this design?Multiple choiceEasy
  50. Why are exceptions part of API design, not just syntax?Multiple choiceEasy