🌱 Spring Boot MCQ Questions – Page 46
Questions 901–920 of 971 total — Spring Boot interview practice
▶ Practice All Spring Boot QuestionsWhat error will occur during application startup due to the use of `@Qualifier`?
java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
public interface MyService {
String serve();
}
@Service("firstImpl")
public class MyServiceImplA implements MyService {
@Override public String serve() { return "Service A"; }
}
@Service
public class ConsumerService {
@Autowired
@Qualifier("nonExistentImpl") // This qualifier does not match any bean
private MyService myService;
public String useService() { return myService.serve(); }
}
What does this code print to the console?
java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Component;
@Component class ServiceA {
public String getInfo() { return "Data from ServiceA."; }
}
@Component class ClientB {
private final ServiceA serviceA;
public ClientB(ServiceA serviceA) { this.serviceA = serviceA; }
public String fetchData() { return "Client fetched: " + serviceA.getInfo(); }
}
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
var context = SpringApplication.run(DemoApplication.class, args);
var client = context.getBean(ClientB.class);
System.out.println(client.fetchData());
}
}
What is the expected outcome when Spring Boot attempts to start with this configuration?
java
package com.example.app;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
@Configuration
@Service
public class AppConfigService {
public String getConfigInfo() {
return "Config info";
}
}
@Service
public class ConsumerService {
@Autowired
private AppConfigService configService;
public String getData() {
return configService.getConfigInfo();
}
}
What is the output of this code?
java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Component;
@Component
class Printer { public String print() { return "Printed by Spring!"; } }
@SpringBootApplication
public class App3 implements CommandLineRunner {
@Autowired private Printer printer;
public static void main(String[] args) { SpringApplication.run(App3.class, args); }
@Override
public void run(String... args) { System.out.println(printer.print()); }
}
What is the HTTP response body when a GET request is made to '/hello'?
java
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String sayHello() {
return "Hello Spring Boot!";
}
}
What is the output of this code when a GET request is made to '/users/101'?
java
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@RequestMapping("/users/{id}")
public String getUserById(@PathVariable String id) {
return "User ID: " + id;
}
}
What error will Spring Boot report when attempting to start this application?
java
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
// Missing @Configuration annotation
public class MyConfig {
public MyComponent myComponent() { return new MyComponent("Configured Data"); }
}
public class MyComponent {
private String data;
public MyComponent(String data) { this.data = data; }
public String getData() { return data; }
}
@Service
public class MyService {
@Autowired
private MyComponent myComponent;
}
What happens when trying to access `/api/message` in this Spring Boot application?
java
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/api")
public class ApiController {
@RequestMapping("/message")
@ResponseBody
public String getMessage() {
return "Hello from API!";
}
}
What header will be added to the response if the original request included `User-Agent: Chrome`?
java
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class GatewayConfig {
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
return builder.routes()
.route("response_header_route", r -> r.path("/test")
.and().header("User-Agent", ".*Chrome.*")
.filters(f -> f.addResponseHeader("X-Gateway-Processed", "true"))
.uri("http://localhost:8085"))
.build();
}
}
By default, from where does `@SpringBootApplication`'s embedded `@ComponentScan` scan for components?
On which of the following can the `@Value` annotation be effectively used in a Spring component?
Given the `Product` class (with `id` and `name` properties) below, what is the HTTP response body when a POST request is made to '/api/products' with the JSON body `{"id": 5, "name": "Laptop"}`?
java
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
// Assuming Product class is defined as: class Product { private Long id; private String name; // Getters, Setters, default constructor }
@RestController
public class ProductController {
@PostMapping("/api/products")
public String addProduct(@RequestBody Product product) {
return "Added product: " + product.getName() + " with ID " + product.getId();
}
}
What is the HTTP response body when making a GET request to `/greet?name=Alice` with the following controller?
java
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class GreetingController {
@GetMapping("/greet")
@ResponseBody
public String greetUser(@RequestParam String name) {
return "Hello, " + name + "!";
}
}
What is the primary annotation used by Spring Boot to automatically inject dependencies into a component by type?
At which stage of the Spring bean lifecycle does a BeanPostProcessor's postProcessBeforeInitialization method execute?
Considering both a filter and an interceptor are active for the `/resource` endpoint, what is the expected console output for a GET request to `/resource`?
java
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.filter.OncePerRequestFilter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Component
class CombinedFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
System.out.println("Filter: Entering");
filterChain.doFilter(request, response);
System.out.println("Filter: Exiting");
}
}
@Component
class CombinedInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { System.out.println("Interceptor: Pre-handle"); return true; }
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) { System.out.println("Interceptor: Post-handle"); }
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { System.out.println("Interceptor: After-completion"); }
}
@Configuration
class WebConfigWithInterceptors implements WebMvcConfigurer {
private final CombinedInterceptor interceptor;
public WebConfigWithInterceptors(CombinedInterceptor interceptor) { this.interceptor = interceptor; }
@Override
public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(interceptor).addPathPatterns("/resource"); }
}
@RestController
class ResourceController {
@GetMapping("/resource")
public String getResource() {
System.out.println("Controller: Executing resource logic");
return "Resource";
}
}
// Assume CombinedFilter is registered for all paths.
Given `MyCustomId` is a simple POJO without any custom converters, what error occurs when accessing `/data/complexValue123` with the following controller?
java
import org.springframework.web.bind.annotation.*;
class MyCustomId {
private String id;
public MyCustomId(String id) { this.id = id; }
public String getId() { return id; }
}
@RestController
public class ComplexDataController {
@GetMapping("/data/{complexId}")
public String getComplexData(@PathVariable MyCustomId complexId) {
return "Complex Data ID: " + complexId.getId();
}
}
What is the error in this Spring Boot application when it tries to configure `MyContextAwareBean`?
java
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
@Component
public class MyContextAwareBean implements ApplicationContextAware {
private ApplicationContext context;
@Override
public void setApplicationContext(String invalidContext) { // Incorrect parameter type
// This method will not be called by Spring
}
public ApplicationContext getContext() { return context; }
}
What is the expected outcome if a client sends a POST request with `Content-Type: text/plain` and a body `{"message":"hello"}` to the following Spring Boot controller method?
java
import org.springframework.web.bind.annotation.*;
import lombok.Data;
@Data
class MessageDto {
private String message;
}
@RestController
@RequestMapping("/messages")
public class MessageController {
@PostMapping
public String receiveMessage(@RequestBody MessageDto messageDto) {
return "Received: " + messageDto.getMessage();
}
}
What error will occur when Spring Boot tries to start the application with these two components?
java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
class ServiceA {
private final ServiceB serviceB;
@Autowired
public ServiceA(ServiceB serviceB) {
this.serviceB = serviceB;
}
}
@Component
class ServiceB {
private final ServiceA serviceA;
@Autowired
public ServiceB(ServiceA serviceA) {
this.serviceA = serviceA;
}
}