Exception Handling Interview Questions Every Fresher Must Know

Exception Handling Interview Questions Every Fresher Must Know Work through these 12 questions out loud. Open any question for the full model answer, the trap to avoid, and the follow-up a panel usually asks.

  1. What is Optional and when should you use it?

    Use Optional as return type when absence is expected — findById returns Optional<User>. Chain with map/flatMap/orElseThrow. Do not use for fields, method parameters, or Jackson...

    Full answer

  2. When should you use CopyOnWriteArrayList?

    Use for subscriber lists in pub/sub where adds are rare and iteration is constant. Avoid for high-frequency add/remove — use ConcurrentLinkedQueue or synchronized collections....

    Full answer

  3. Multiple beans of the same type exist and Spring throws an error. How do you fix it?

    Spring needs disambiguation when two `@Service` implement the same interface. ```java @Primary @Service public class StripePaymentGateway implements PaymentGateway {} @Service...

    Full answer

  4. What happens if @Transactional fails midway? How do you verify rollback?

    Default: runtime exception marks rollback-only; transaction commits nothing on exit. ```java @Transactional(rollbackFor = Exception.class) public void placeOrder(Order o) {...

    Full answer

  5. What happens if an exception occurs inside a filter or interceptor?

    Exceptions in `Filter` bypass `@ControllerAdvice` unless handled. Filters run before MVC. ```java @Override protected void doFilterInternal(HttpServletRequest req,...

    Full answer

  6. Difference between @Component, @Service, @Repository?

    All register as singleton beans via component scan. Choose by layer for readability: @Repository on DAOs (plus SQLException translation), @Service on business logic, @Component...

    Full answer

  7. Explain try-with-resources and suppressed exceptions.

    try (Connection c = ds.getConnection(); PreparedStatement ps = c.prepareStatement(sql)) { ... } — both closed even on SQLException. Check e.getSuppressed() in logs for close...

    Full answer

  8. How does Bean Validation work in Spring Boot 3?

    DTO fields: @NotBlank, @Email, @Size. Controller: createUser(@Valid @RequestBody UserDto dto). Global @RestControllerAdvice maps MethodArgumentNotValidException to 400 + field...

    Full answer

  9. What is ProblemDetail (RFC 7807) in Spring 6 error handling?

    @ExceptionHandler(NotFoundException.class) ProblemDetail handle(NotFoundException ex) { return ProblemDetail.forStatusAndDetail(NOT_FOUND, ex.getMessage()); } — consistent 404...

    Full answer

  10. What are common mistakes freshers make in Spring Boot projects?

    ```java // ❌ Wrong @RestController public class OrderController { @PostMapping("/orders") public Order create(@RequestBody Order o) { return orderRepository.save(o); // DB in...

    Full answer

  11. Explain the difference between == and .equals() in Java.

    For primitives, == compares values. For objects, == asks if two references point to the same instance. equals() compares content when overridden — String overrides it for...

    Full answer

  12. How do you handle exceptions globally in Spring?

    Create @RestControllerAdvice with @ExceptionHandler(MethodArgumentNotValidException.class) returning 400 + field map, NotFoundException → 404, generic Exception → 500 without...

    Full answer

Exception Handling Interview Questions and Answers · Top 50 Exception Handling Interview Questions and Answers (2026 Edition) · Top 40 Exception Handling Interview Questions | Must-Prepare before interview. · Exception Handling Interview Questions for 2–3 Years

All interview question lists · Interview guides · Home · Support the platform