🌱 Spring Boot MCQ Questions – Page 45

Questions 881–900 of 971 total — Spring Boot interview practice

▶ Practice All Spring Boot Questions
Q881 medium code error
What will be the outcome when running this Spring Boot application?
java
public interface Printer {
    String print();
}

@Configuration
public class AppConfig {
    @Bean
    public Printer inkjetPrinter() { // Bean named "inkjetPrinter" by default
        return () -> "Printing with Inkjet";
    }
}

@Component
public class PrintManager {
    @Autowired
    @Qualifier("laserPrinter") // No bean named "laserPrinter"
    private Printer printer;

    public void managePrint() {
        System.out.println(printer.print());
    }
}
Q882 medium
Which Spring Boot annotation is used to map an HTTP POST request to a specific handler method in a REST controller?
Q883 medium code output
Given the Spring Boot controller above, what will be the HTTP response body (or behavior) when a GET request is made to `/item/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 ItemController {
    @GetMapping("/item/{itemId}")
    public String getItem(@PathVariable int itemId) {
        return "Retrieved item " + itemId;
    }
}
Q884 medium code output
What is the HTTP response body when a GET request is made to `/items/AXB123`?
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 ItemController {
    @GetMapping("/items/{sku}")
    public String getItemDetails(@PathVariable("sku") String productIdentifier) {
        return "Item SKU: " + productIdentifier;
    }
}
Q885 medium code error
What kind of error will occur when a client attempts to send a JSON request body `{"customerName": "Alice", "quantity": 2}` to this POST endpoint?
java
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/orders")
public class OrderController {
    @PostMapping
    public String placeOrder(@RequestParam("order") OrderDto order) {
        return "Order received for: " + order.getCustomerName();
    }
}

class OrderDto {
    private String customerName;
    private int quantity;
    public String getCustomerName() { return customerName; }
    public void setCustomerName(String customerName) { this.customerName = customerName; }
    public int getQuantity() { return quantity; }
    public void setQuantity(int quantity) { this.quantity = quantity; }
}
Q886 medium
Which annotation is used in Spring Boot to extract values from the URI path and bind them to method parameters?
Q887 medium
What happens if a `@RestController` method returns `ModelAndView`?
Q888 medium
What is the primary purpose of the `@Service` annotation in Spring Boot?
Q889 medium
How can you inject all beans of a specific interface type (e.g., `PaymentProcessor`) into a `List` using `@Autowired`?
Q890 medium code error
Consider the following Spring component. What error will occur during application startup?
java
java
@Component
public class IdGenerator {
    @Value("#{"".trim()}")
    private Long nextId;

    public Long getNextId() {
        return nextId;
    }
}
Q891 medium code output
Given the `Product` class with an `ItemType` enum, what will be the value of `product.getItemType()` after a form submission 'productName=Laptop&itemType=ELECTRONICS'?
java
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ModelAttribute;

// ItemType.java
public enum ItemType {
    ELECTRONICS, CLOTHING, BOOKS
}

// Product.java
public class Product {
    private String productName; private ItemType itemType;
    public String getProductName() { return productName; }
    public void setProductName(String productName) { this.productName = productName; }
    public ItemType getItemType() { return itemType; }
    public void setItemType(ItemType itemType) { this.itemType = itemType; }
}

@Controller
public class ProductController {
    @PostMapping("/product/add")
    public String addProduct(@ModelAttribute Product product) {
        // product object is processed here
        return "product_details";
    }
}
Q892 medium
When would you typically use the `name` (or `value`) attribute within the `@RequestParam` annotation?
Q893 medium
When the `ApplicationContext` is shutting down, which mechanism allows beans to perform cleanup operations?
Q894 medium code error
What error will occur when Spring Boot attempts to initialize MyService?
java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

class UnmanagedDependency {
    public String getData() { return "data"; }
}

@Component
class MyService {
    @Autowired
    private UnmanagedDependency dependency;

    public String getInfo() { return dependency.getData(); }
}
Q895 medium code error
What is the expected error when accessing this endpoint with GET /hello?
java
import org.springframework.web.bind.annotation.*;

@RestController
public class GreetingController {

    @GetMapping("/hello")
    public String sayHello(@RequestParam(required = true) String name) {
        return "Hello, " + name + "!";
    }
}
Q896 medium code output
What does this code print to the console when run as a Spring Boot application?
java
import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@Component
class ServiceB { public ServiceB() { System.out.println("ServiceB created."); } }

@Component
class ClientB implements CommandLineRunner {
    private final ServiceB serviceB;
    public ClientB(@Autowired ServiceB serviceB) {
        this.serviceB = serviceB;
        System.out.println("ClientB initialized with ServiceB.");
    }
    @Override
    public void run(String... args) { /* no-op */ }
}

@SpringBootApplication
public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
Q897 medium code output
Assuming a Spring Boot application with the following controller, what is the outcome regarding the view and model attributes if a request is made to `/empty`?
java
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.ui.Model;

@Controller
public class EmptyController {

    @GetMapping("/empty")
    public String showEmptyPage(Model model) {
        // No attributes are added to the model
        return "emptyPage";
    }
}
Q898 medium
What is the result of `ResponseEntity.ok().build()` when returned from a Spring Boot controller?
Q899 medium code output
Given the Spring Boot controller above, what will be the HTTP response body when a GET request is made to `/greeting`?
java
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SimpleController {
    @GetMapping("/greeting")
    public String greet() {
        return "Hello from Spring Boot!";
    }
}
Q900 medium code error
What kind of error will Spring Boot produce when attempting to instantiate `MyService`?
java
import org.springframework.stereotype.Service;

// MyDependency is NOT a Spring bean
class MyDependency { public String getValue() { return "Dep Value"; } }

@Service
public class MyService {
    private MyDependency myDependency;
    private String name;

    public MyService(MyDependency myDependency) { this.myDependency = myDependency; }
    public MyService(String name) { this.name = name; }
    public String getResult() { return "Service result"; }
}
← Prev 4344454647 Next → Page 45 of 49 · 971 questions