🌱 Spring Boot MCQ Questions – Page 27
Questions 521–540 of 971 total — Spring Boot interview practice
▶ Practice All Spring Boot QuestionsWhat is the Eureka registration status of a Spring Boot application using `@EnableEurekaClient` if its `application.properties` contains the following?
properties
spring.application.name=my-app
server.port=8081
eureka.client.enabled=false
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka
What is the output when a Spring Boot application resolves an ambiguous `@Service` dependency using `@Qualifier`? (Assume standard Spring Boot setup and necessary imports.)
java
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Qualifier;
interface Processor { String execute(); }
@Service("fastProcessor") class FastProcessor implements Processor { public String execute() { return "Fast Result"; } }
@Service("slowProcessor") class SlowProcessor implements Processor { public String execute() { return "Slow Result"; } }
// In a @SpringBootApplication's CommandLineRunner, after autowiring:
// @Autowired @Qualifier("slowProcessor") Processor processor;
// System.out.println(processor.execute());
Assuming a user is authenticated and attempts to send a POST request to `/submit` without including a valid CSRF token in the request headers or parameters. What is the expected HTTP status code and response?
java
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import static org.springframework.security.config.Customizer.withDefaults;
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http.authorizeHttpRequests(authorize -> authorize.anyRequest().authenticated())
.formLogin(withDefaults()); // Enables form login and default CSRF
return http.build();
}
@Bean
public UserDetailsService userDetailsService() {
UserDetails user = User.withUsername("user").password("{noop}password").roles("USER").build();
return new InMemoryUserDetailsManager(user);
}
}
@RestController
public class MyController {
@PostMapping("/submit")
public String submitData(@RequestBody String data) { return "Data submitted: " + data; }
}
A Spring Boot application has `application.properties` with `server.port=8081`. It is deployed using the following Dockerfile. What port will the application be accessible on externally if `docker run -p 80:8080 my-image` is executed?
dockerfile
FROM openjdk:17-jre-slim
WORKDIR /app
COPY target/my-app.jar app.jar
COPY src/main/resources/application.properties .
EXPOSE 8080
CMD ["java", "-jar", "app.jar"]
What will happen if a POST request with a JSON body `{"value": "test"}` is made to `/api/submit` without any parameter binding annotation?
java
import org.springframework.web.bind.annotation.*;
@RestController
public class MyController {
@PostMapping("/api/submit")
public String submitData(MyData data) { // Missing @RequestBody
return "Received: " + data.getValue();
}
static class MyData {
private String value;
public String getValue() { return value; }
public void setValue(String value) { this.value = value; }
}
}
What will happen when a Spring Boot application 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.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
interface MyInterface {}
@Service
class MyServiceImpl implements MyInterface {
// Implementation details
}
@SpringBootApplication
public class MyApplication {
@Autowired
private MyInterface myInterface;
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
Given the following DTOs and controller, what would be the return value of `submitOrder` if the POST request body is `{"orderId":"O123", "customer":{"name":"Bob", "email":"bob@example.com"}}`?
java
import org.springframework.web.bind.annotation.*;
import lombok.Data;
@Data
class CustomerDto {
private String name;
private String email;
}
@Data
class OrderDto {
private String orderId;
private CustomerDto customer;
}
@RestController
@RequestMapping("/orders")
public class OrderController {
@PostMapping
public String submitOrder(@RequestBody OrderDto orderDto) {
return "Order ID: " + orderDto.getOrderId() + ", Customer: " + orderDto.getCustomer().getName();
}
}
Which of the following statements best describes the typical distinction between a class annotated with `@Service` and a class annotated with `@Repository`?
What error will occur when this Spring Boot application attempts to start?
java
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyController {
@GetMapping(value = "/api/test", headers = {"X-Custom-Header=value", "Content-Type!"})
public String testHeaders() {
return "Header check";
}
}
What will be the behavior when accessing `/product` without providing a `name` parameter?
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 ProductController {
@GetMapping("/product")
@ResponseBody
public String getProduct(@RequestParam String name) {
return "Product: " + name;
}
}
What issue will arise when Spring Boot attempts to create and use the following services?
java
package com.example.app;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
interface NotificationService {
String send(String message);
}
@Service("emailService")
class EmailNotificationService implements NotificationService {
@Override
public String send(String message) { return "Email: " + message; }
}
@Service("smsService")
class SmsNotificationService implements NotificationService {
@Override
public String send(String message) { return "SMS: " + message; }
}
@Service
class ClientService {
@Autowired
private NotificationService notificationService;
}
What error occurs when accessing `/products/abc` with the following controller?
java
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/products")
public class ProductController {
@GetMapping("/{productId}")
public String getProduct(@PathVariable int productId) {
return "Product ID: " + productId;
}
}
A Eureka server is configured with the following `application.yml`. What specific log message regarding self-preservation mode will typically appear during the Eureka server's startup?
yaml
spring:
application:
name: eureka-server-prod
server:
port: 8761
eureka:
client:
register-with-eureka: false
fetch-registry: false
server:
enable-self-preservation: false
What error occurs during application startup with the following controller?
java
import org.springframework.web.bind.annotation.*;
@RestController
public class ResourceController {
@GetMapping("/resource/{id}")
public String getResourceById(@PathVariable String id) {
return "Resource by ID: " + id;
}
@GetMapping("/resource/{name}")
public String getResourceByName(@PathVariable String name) {
return "Resource by Name: " + name;
}
}
Given this `docker-compose.yml`, what will be the name of the PostgreSQL container if the project directory is named `myproject`?
yaml
version: '3.8'
services:
app:
image: spring-boot-app
ports:
- "8080:8080"
environment:
SPRING_DATASOURCE_URL: jdbc:postgresql://db:5432/mydb
db:
image: postgres:13
environment:
POSTGRES_DB: mydb
POSTGRES_USER: user
POSTGRES_PASSWORD: password
Consider the following bean definitions creating a circular dependency. What error will be thrown during application startup?
java
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
public class BeanA { public BeanA(BeanB b) {} }
public class BeanB { public BeanB(BeanA a) {} }
@Configuration
public class CircularConfig {
@Bean
public BeanA beanA(BeanB beanB) {
return new BeanA(beanB);
}
@Bean
public BeanB beanB(BeanA beanA) {
return new BeanB(beanA);
}
}
When a Spring Boot controller method needs to return a custom HTTP status code, specific headers, and a response body, which return type is most appropriate and flexible?
Given the Spring Boot component and `application.properties` snippet, what error will occur when the application attempts to start?
java
java
@Component
public class AppConfig {
@Value("${app.greeting}")
private String greetingMessage;
public String getGreetingMessage() {
return greetingMessage;
}
}
properties
# application.properties (file does NOT contain 'app.greeting')
app.name=MyApplication
What is the output of this code when the `main` method of `MyApplication` is executed? (Assume default logging is suppressed, focus on `System.out`)
java
// src/main/java/com/example/demo/MyApplication.java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
@Bean
public CommandLineRunner run() {
return args -> {
System.out.println("Application context fully loaded.");
};
}
}
// src/main/java/com/example/demo/ComponentA.java
package com.example.demo;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
@Component
class ComponentA {
@PostConstruct
public void init() {
System.out.println("ComponentA Initialized");
}
}
// src/main/java/com/example/demo/ComponentB.java
package com.example.demo;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
@Component
class ComponentB {
@PostConstruct
public void init() {
System.out.println("ComponentB Initialized");
}
}
What is the output of this Spring Boot code snippet?
java
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class MathService {
@Value("#{T(java.lang.Math).round(T(java.lang.Math).PI)}")
private int roundedPi;
public int getRoundedPi() {
return roundedPi;
}
}
// In a Spring Boot application, if this component is scanned:
// MathService service = applicationContext.getBean(MathService.class);
// System.out.println(service.getRoundedPi());