0
0
Spring Bootframework~20 mins

Returning different status codes in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Status Code Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What HTTP status code does this Spring Boot controller return?
Consider this Spring Boot controller method. What status code will the client receive when calling this endpoint?
Spring Boot
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {
    @GetMapping("/check")
    public ResponseEntity<String> check() {
        return ResponseEntity.status(201).body("Created");
    }
}
A200 OK
B404 Not Found
C201 Created
D500 Internal Server Error
Attempts:
2 left
💡 Hint
Look at the status method used in ResponseEntity.
📝 Syntax
intermediate
2:00remaining
Which option correctly returns a 404 status with a message in Spring Boot?
You want to return a 404 Not Found status with a message "User not found". Which code snippet is correct?
Areturn ResponseEntity.status(404).body("User not found");
Breturn ResponseEntity.notFound().body("User not found");
Creturn ResponseEntity.status(HttpStatus.NOT_FOUND, "User not found");
Dreturn new ResponseEntity<>("User not found", 404);
Attempts:
2 left
💡 Hint
Check the ResponseEntity methods and constructors.
🔧 Debug
advanced
2:00remaining
Why does this Spring Boot controller always return 200 OK instead of 400 Bad Request?
Look at this controller method. It tries to return 400 Bad Request on error but always returns 200 OK. Why?
Spring Boot
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ErrorController {
    @GetMapping("/error")
    public ResponseEntity<String> error(boolean hasError) {
        if (hasError) {
            return ResponseEntity.status(400).body("Bad Request");
        }
        return ResponseEntity.ok("All good");
    }
}
AThe 400 ResponseEntity is created but not returned from the method.
BResponseEntity.status(400) is invalid and defaults to 200.
CThe method parameter 'hasError' is ignored by Spring Boot.
DThe method must throw an exception to return 400 status.
Attempts:
2 left
💡 Hint
Check if the method returns the ResponseEntity created inside the if block.
state_output
advanced
2:00remaining
What is the HTTP status code when this Spring Boot controller returns null body with 204 status?
This controller returns ResponseEntity with status 204 No Content and a null body. What will the client receive?
Spring Boot
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class NoContentController {
    @GetMapping("/nocontent")
    public ResponseEntity<String> noContent() {
        return ResponseEntity.noContent().build();
    }
}
A500 Internal Server Error
B204 No Content with 'null' string body
C200 OK with empty body
D204 No Content with empty body
Attempts:
2 left
💡 Hint
Check what ResponseEntity.noContent().build() does.
🧠 Conceptual
expert
3:00remaining
Which option correctly demonstrates returning different status codes based on input in Spring Boot?
You want a controller method that returns 200 OK with "Success" if input is true, and 404 Not Found with "Not Found" if false. Which code snippet correctly implements this?
A
public ResponseEntity&lt;String&gt; check(boolean input) {
    return input ? ResponseEntity.ok("Success") : ResponseEntity.notFound().build();
}
B
public ResponseEntity&lt;String&gt; check(boolean input) {
    if (input) {
        return ResponseEntity.ok("Success");
    } else {
        return ResponseEntity.status(404).body("Not Found");
    }
}
C
public ResponseEntity&lt;String&gt; check(boolean input) {
    if (input) {
        return new ResponseEntity&lt;&gt;("Success", HttpStatus.OK);
    } else {
        return new ResponseEntity&lt;&gt;("Not Found", HttpStatus.NOT_FOUND);
    }
}
D
public ResponseEntity&lt;String&gt; check(boolean input) {
    if (input) {
        return ResponseEntity.ok("Success");
    }
    return ResponseEntity.notFound().body("Not Found");
}
Attempts:
2 left
💡 Hint
Check which options correctly set both status and body for 404 response.