Bird
0
0

Given this @ControllerAdvice class snippet, what will be the HTTP status code returned when a NullPointerException occurs?

medium📝 component behavior Q4 of 15
Spring Boot - Exception Handling
Given this @ControllerAdvice class snippet, what will be the HTTP status code returned when a NullPointerException occurs?
@ControllerAdvice
public class GlobalExceptionHandler {
  @ExceptionHandler(NullPointerException.class)
  public ResponseEntity handleNullPointer(NullPointerException ex) {
    return new ResponseEntity<>("Null value found", HttpStatus.BAD_REQUEST);
  }
}
A404 Not Found
B500 Internal Server Error
C400 Bad Request
D200 OK
Step-by-Step Solution
Solution:
  1. Step 1: Identify the exception handled

    The method handles NullPointerException specifically.
  2. Step 2: Check the ResponseEntity status code

    The method returns HttpStatus.BAD_REQUEST which corresponds to 400.
  3. Final Answer:

    400 Bad Request -> Option C
  4. Quick Check:

    NullPointerException handled with 400 status [OK]
Quick Trick: Exception handler returns status in ResponseEntity [OK]
Common Mistakes:
  • Assuming default 500 error without checking handler
  • Confusing 404 Not Found with bad request
  • Thinking 200 OK is returned automatically

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes