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 3 of 4)
- Why might you choose to propagate an exception to a higher layer rather than handle it immediately?Multiple choiceHard
- You are designing a method that takes a user ID as input. What should you do if the ID is null?Multiple choiceMedium
- In the context of exception handling, what does 'state protection' mean?Multiple choiceMedium
- In designing a custom exception, which practice is recommended to provide meaningful diagnostics?Multiple choiceHard
- When should you choose to not catch an exception in your code?Multiple choiceMedium
- In which scenario does a 'finally' block execute?Multiple choiceEasy
- Which statement is true about exception handling in Java?Multiple choiceMedium
- Which exception type should you typically throw for invalid method arguments?Multiple choiceEasy
- What block should be used to handle exceptions thrown by a try block?Multiple choiceEasy
- Why might you choose to extend Exception rather than RuntimeException for a custom exception?Multiple choiceMedium
- Why do teams use unchecked exceptions for certain errors in a Java application in production code?Multiple choiceMedium
- Which Java keyword is used to define a block of code that can throw an exception?Multiple choiceEasy
- Which of the following is a valid reason to catch and rethrow an exception as a different type?Multiple choiceMedium
- In Java, what is a recommended practice when logging exceptions for debugging?Multiple choiceMedium
- When might a checked exception be justified?Multiple choiceMedium
- When should a method catch an exception and take no further action?Multiple choiceMedium
- Why is it important to declare checked exceptions in the method signature?Multiple choiceEasy
- Which is an anti-pattern in catch blocks?Multiple choiceHard
- In a large e-commerce system, why might you create a hierarchy of custom exceptions?Multiple choiceHard
- When designing a custom exception, which of the following practices is recommended?Multiple choiceHard
- In a try-catch-finally structure, which order do the blocks execute if no exception is thrown?Multiple choiceMedium
- In Java, what is a typical mistake developers make when wrapping exceptions?Multiple choiceMedium
- Which statement is true about the 'finally' block in exception handling?Multiple choiceMedium
- In Java, which practice helps ensure that errors are detected early in the program flow?Multiple choiceEasy
- Why is it beneficial to log exceptions with their causes in an e-commerce application?Multiple choiceMedium
- In a checkout application, what is the best way to handle a temporary database connection failure during order processing?Multiple choiceHard
- In which scenario is it most appropriate to use fail-fast validation in a constructor?Multiple choiceHard
- Which Java keyword is used to define a custom exception class?Multiple choiceMedium
- In which scenario is it inappropriate to use a custom exception?Multiple choiceMedium
- How would you explain the key difference between a checked exception and an unchecked exception in an interview?Multiple choiceEasy
- How should a method handle exceptions internally if it decides not to propagate them?Multiple choiceHard
- Which statement best explains why unchecked exceptions exist?Multiple choiceMedium
- What happens if a method throws a checked exception that is not handled or declared?Multiple choiceEasy
- A repository catches SQLException. What is the strongest reason to do that?Multiple choiceEasy
- Which principle is important when deciding between a checked and unchecked exception for a custom exception?Multiple choiceHard
- Which exception type should be used to indicate a logic error?Multiple choiceEasy
- How would you explain a key difference between checked and unchecked exceptions in a Java interview?Multiple choiceHard
- What keyword is used to manually throw an exception in Java?Multiple choiceEasy
- How would you explain an anti-pattern in error handling when dealing with Java exceptions in an interview?Multiple choiceHard
- Why validate an empty cart or invalid quantity before contacting inventory or payment systems?Multiple choiceMedium
- What should a method that declares a checked exception be responsible for?Multiple choiceEasy
- In a try-catch-finally block, if an exception is thrown in the catch block, what is executed?Multiple choiceHard
- Why does rejecting invalid input early in a method matter in practice?Multiple choiceMedium
- Why does a 'try' block matter in practice?Multiple choiceMedium
- Why does a catch block in Java matter in practice?Multiple choiceEasy
- In an e-commerce checkout system, which exception type should you use for handling invalid payment details?Multiple choiceMedium
- Why is it important to include context in error messages?Multiple choiceMedium
- Which design consideration is crucial when creating a custom exception hierarchy?Multiple choiceHard
- Why should you create a custom exception in Java?Multiple choiceEasy
- Why is it important to use fail-fast validation in a constructor?Multiple choiceEasy