🌱 Spring Boot MCQ Questions – Page 36

Questions 701–720 of 971 total — Spring Boot interview practice

▶ Practice All Spring Boot Questions
Q701 medium code error
Given the following Spring Boot controller that uses `@SessionAttributes`. What error will occur on a subsequent request to `/editUser` if the initial request to `/showUserForm` does not add an object named 'userForm' to the model?
java
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.Model;

@Controller
@SessionAttributes("userForm")
public class UserController {

    // This method is missing model.addAttribute("userForm", new UserForm());
    @GetMapping("/showUserForm")
    public String showUserForm() {
        return "userForm"; 
    }

    @GetMapping("/editUser")
    public String editUserForm(Model model) {
        // This method expects 'userForm' to be in session now
        UserForm form = (UserForm) model.getAttribute("userForm");
        return "editForm";
    }
}
Q702 medium
Can you define regular expressions for path variables in Spring Boot?
Q703 medium
Upon successful creation of a new resource via an HTTP POST request, what is the most appropriate HTTP status code to return to the client, especially when the URI of the newly created resource is provided in the response?
Q704 medium code output
What is the output when accessing `/user/details?username=johndoe&age=30`?
java
import org.springframework.web.bind.annotation.*;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@RestController
public class MyApp {
    public static void main(String[] args) {
        SpringApplication.run(MyApp.class, args);
    }

    @GetMapping("/user/details")
    public String getUserDetails(
        @RequestParam String username,
        @RequestParam(required = false, defaultValue = "N/A") String email,
        @RequestParam(defaultValue = "0") int age) {
        
        return String.format("User: %s, Email: %s, Age: %d", username, email, age);
    }
}
Q705 medium
To extract a query parameter named `id` from a URL like `/items?id=123` within a `@Controller` method in Spring Boot, which annotation would you use on the method parameter?
Q706 medium code output
What is the output of the `MyApplication` when executed?
java
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
class ExternalDependency {
    public String getData() { return "Data from ExternalDependency"; }
}

class MyConfiguredBean {
    private String data;
    public MyConfiguredBean(String data) { this.data = data; }
    public String getInfo() { return "Configured with: " + data; }
}

@Configuration
class AppConfig {
    @Bean
    public MyConfiguredBean myConfiguredBean(ExternalDependency dep) { // Autowired by type
        return new MyConfiguredBean(dep.getData());
    }
}

@Component
class MyRunner implements CommandLineRunner {
    @Autowired
    private MyConfiguredBean configuredBean;

    @Override
    public void run(String... args) {
        System.out.println(configuredBean.getInfo());
    }
}

@SpringBootApplication
public class MyApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}
Q707 medium code output
Consider a Spring Boot REST controller with a `@PostMapping("/register")` method that accepts a `@Valid @RequestBody UserRegistrationDto userDto`. What is the HTTP status code and response body content when the following JSON request is sent to `/register`? `POST /register HTTP/1.1 Content-Type: application/json { "username": null, "email": "invalid-email", "age": 0 }`
java
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Email;
import javax.validation.constraints.Min;

public class UserRegistrationDto {
    @NotNull(message = "Username must not be null")
    private String username;

    @NotNull(message = "Email must not be null")
    @Email(message = "Email format is invalid")
    private String email;

    @NotNull(message = "Age must not be null")
    @Min(value = 1, message = "Age must be at least 1")
    private Integer age;

    // Getters and setters are implicitly present
}
Q708 medium code output
What is the HTTP status code and response body if the 'id' parameter is 0?
java
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {
    @GetMapping("/item")
    public ResponseEntity<String> getItem(@RequestParam int id) {
        if (id == 0) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Item not found.");
        } else {
            return ResponseEntity.ok("Item " + id);
        }
    }
}
Q709 medium
What happens if `@Autowired` is used for a field, no matching bean is found, and `required` is set to its default value (true)?
Q710 medium code output
What is the output of this code when a POST request is made to '/content' with 'Content-Type: application/json' and body '{"message":"hello"}'?
java
import org.springframework.web.bind.annotation.*;

@RestController
public class ContentController {
    @RequestMapping(value = "/content", method = RequestMethod.POST, consumes = "application/json")
    public String receiveJson(@RequestBody String body) {
        return "JSON received: " + body;
    }

    @RequestMapping(value = "/content", method = RequestMethod.POST, consumes = "text/plain")
    public String receiveText(@RequestBody String body) {
        return "Text received: " + body;
    }
}
Q711 medium
What is a common way to handle exceptions globally across multiple methods or even multiple `@RestController` classes in a Spring Boot application?
Q712 medium
What happens when the `ApplicationContext` is 'refreshed' in Spring Boot?
Q713 medium code output
Consider a Spring Boot application that includes `@EnableScheduling`. What is the output related to `someScheduledTask` when the application starts up?
java
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class SchedulingDisabler {

    @Scheduled(fixedRate = 5000)
    public void someScheduledTask() {
        System.out.println("This should never print if scheduling is disabled.");
    }
}
Q714 medium code output
What is the expected outcome when this Spring Boot application starts?
java
import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.CommandLineRunner;

class NonSpringService { // Not a @Component
    public String say() { return "I am not a bean."; }
}

@Component
class MyRunner implements CommandLineRunner {
    @Autowired
    private NonSpringService service;

    @Override
    public void run(String... args) {
        System.out.println(service.say());
    }
}

@SpringBootApplication
public class MyApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}
Q715 medium
When defining a custom service method that performs a delete operation (e.g., `repository.deleteById(id)`), what is a common best practice regarding transaction management in Spring Boot?
Q716 medium code output
What is the HTTP response body (or part of it) when a GET request is made to '/users/invalid_id'?
java
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UsersController {
    @GetMapping("/users/{id}")
    public String getUserById(@PathVariable Long id) {
        return "User ID: " + id;
    }
}
Q717 medium
Can `@Qualifier` be used to filter or select specific beans when autowiring a `List<MyService>` or `Map<String, MyService>`?
Q718 medium
To define a custom scope (e.g., `prototype`, `request`, `session`) for a bean created using `@Bean`, which additional annotation should be used alongside `@Bean` on the method?
Q719 medium code error
A client makes a GET request to `/api/orders/details/123`. The `OrderDetail` class is a complex custom object. What error will occur?
java
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
public class OrderController {
    @GetMapping("/orders/details/{orderDetailId}")
    public String getOrderDetails(@PathVariable OrderDetail orderDetail) {
        return "Order detail ID: " + orderDetail.getId();
    }
}
class OrderDetail {
    private String id;
    public String getId() { return id; }
    public void setId(String id) { this.id = id; }
    public OrderDetail() {}
}
Q720 medium
Which HTTP status code would a client most likely receive if they request `/users/abc` when the expected `@PathVariable` is an `Integer` in a method mapped to `/users/{id}`?
← Prev 3435363738 Next → Page 36 of 49 · 971 questions