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