🌱 Spring Boot MCQ Questions – Page 42
Questions 821–840 of 971 total — Spring Boot interview practice
▶ Practice All Spring Boot QuestionsWhich of the following is NOT a common use case for `ResponseEntity` in a Spring Boot application?
What error will occur when Spring Boot tries to initialize the following component?
java
java
@Component
public class ToggleSwitch {
@Value("#{'true' + 'false'}")
private boolean enabled;
public boolean isEnabled() {
return enabled;
}
}
In the `@RequestMapping("/products/{id}")` annotation, what does `{id}` represent?
What will be the outcome when running this Spring Boot application?
java
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
public class DataStore {
private String type;
public DataStore(String type) { this.type = type; }
public String getType() { return type; }
}
@Configuration
public class StoreConfig {
@Bean("mysqlStore")
public DataStore databaseStore() { // Bean named "mysqlStore"
return new DataStore("MySQL");
}
}
@Component
public class DataManager {
@Autowired
@Qualifier("databaseStore") // Tries to use the method name as qualifier, but bean has explicit name
private DataStore store;
public void printStoreType() {
System.out.println(store.getType());
}
}
Given `application.properties` contains `app.name=WebApp` and the following Java code, what does it print?
java
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class AppInfoService {
@Value("#{'Application: ' + '${app.name}'.toUpperCase()}")
private String info;
public String getInfo() {
return info;
}
}
// In a Spring Boot application, if this component is scanned:
// AppInfoService service = applicationContext.getBean(AppInfoService.class);
// System.out.println(service.getInfo());
Consider the Spring Boot application above. If it's packaged as `myapp.jar` and executed from the command line as `java -jar myapp.jar --app.message='Hello JAR!'`, what will be the output from the `CommandLineRunner`?
java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class MyApplication {
@Value("${app.message:Default Message}")
private String appMessage;
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
@Bean
public CommandLineRunner runner() {
return args -> {
System.out.println("App Message: " + appMessage);
};
}
}
What is the primary function of a `ViewResolver` in Spring MVC?
What kind of error will occur when Spring Boot tries to start an application containing these two services?
java
import org.springframework.stereotype.Service;
@Service
public class ServiceA {
private ServiceB serviceB;
public ServiceA(ServiceB serviceB) { this.serviceB = serviceB; }
public String getMessage() { return "From A"; }
}
@Service
public class ServiceB {
private ServiceA serviceA;
public ServiceB(ServiceA serviceA) { this.serviceA = serviceA; }
public String getMessageFromB() { return "From B"; }
}
What is the primary role of the `@Repository` annotation in a Spring Boot application?
Consider the `User` DTO and controller method below. What error occurs if a client sends a POST request with `{"email": "test@example.com"}` and `Content-Type: application/json`?
java
package com.example.demo;
import jakarta.validation.constraints.NotBlank;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.ResponseEntity;
import jakarta.validation.Valid;
public class User {
@NotBlank
private String username;
private String email;
// Getters and Setters, default constructor
}
@RestController
public class UserController {
@PostMapping("/users")
public ResponseEntity<String> createUser(@Valid @RequestBody User user) {
return ResponseEntity.ok("User created.");
}
}
A client sends a POST request to `/items` with `Content-Type: application/json` and a malformed JSON body `{"name": "Test", "value": }`. What error will the following controller method produce?
java
package com.example.demo;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.ResponseEntity;
@RestController
public class ItemController {
@PostMapping("/items")
public ResponseEntity<String> createItem(@RequestBody Item item) {
return ResponseEntity.ok("Item created.");
}
}
// Item class has default constructor, private String name; private int value;
Given the `ValueDto` and controller below, what is the expected outcome if a POST request body is `{"num": "twenty"}` (string instead of integer)?
java
import org.springframework.web.bind.annotation.*;
import lombok.Data;
@Data
class ValueDto {
private int num;
}
@RestController
@RequestMapping("/values")
public class ValueController {
@PostMapping
public String processValue(@RequestBody ValueDto valueDto) {
return "Processed num: " + valueDto.getNum();
}
}
By default, when using `@RequestBody` in a Spring Boot controller method, is the HTTP request body considered optional or required?
If a request is made to `/start`, what will be printed to the console when the `/next` endpoint is subsequently processed due to the redirect?
java
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.ui.Model;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
@Controller
public class FirstController {
@GetMapping("/start")
public String startProcess(Model model) {
model.addAttribute("tempMessage", "This should not pass.");
return "redirect:/next";
}
@GetMapping("/next")
public String nextPage(Model model) {
System.out.println("Model attributes in /next: " + model.asMap().keySet());
return "nextView";
}
}
Consider the following Spring Boot controller method. What error will be thrown if a form submission to `/saveUser` includes validation errors?
java
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
@Controller
public class UserController {
@PostMapping("/saveUser")
public String saveUser(BindingResult result, @Validated @ModelAttribute UserForm userForm) {
if (result.hasErrors()) {
return "userForm";
}
return "success";
}
}
Given the `Item` class with `private int quantity;`, a client sends `Content-Type: application/json` and body `{"name": "Book", "quantity": "ten"}` to the `/items` endpoint. What error occurs?
java
package com.example.demo;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.ResponseEntity;
@RestController
public class ItemController {
@PostMapping("/items")
public ResponseEntity<String> createItem(@RequestBody Item item) {
return ResponseEntity.ok("Item created: " + item.getName());
}
}
// Item class has default constructor, private String name; private int quantity;
What will be the outcome when running this Spring Boot application?
java
public interface Converter {
String convert(String input);
}
public class MyConverter implements Converter { // No Spring annotation
@Override
public String convert(String input) {
return "Converted: " + input.toUpperCase();
}
}
@Component
public class DataImporter {
@Autowired
@Qualifier("myConverter") // Trying to inject non-Spring managed bean
private Converter converter;
public void importData(String data) {
System.out.println(converter.convert(data));
}
}
By default, how does Spring resolve dependencies when using `@Autowired`?
A client sends a POST request to `/api/data` with a valid JSON body. However, the `MyData` class lacks a default constructor. What error will occur during the deserialization of the request body?
java
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api")
public class DataController {
@PostMapping("/data")
public String processData(@RequestBody MyData data) {
return "Processed: " + data.getValue();
}
}
class MyData {
private String value;
public MyData(String value) { this.value = value; }
public String getValue() { return value; }
public void setValue(String value) { this.value = value; }
}
If a client sends a PUT request to `/api/config/name` with a JSON body `{"value": "new_value"}`, what kind of error will occur during deserialization of `ConfigItem`?
java
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api/config")
public class ConfigController {
@PutMapping("/{key}")
public ResponseEntity<ConfigItem> updateConfig(@PathVariable String key, @RequestBody ConfigItem configItem) {
// Logic to update config item
return ResponseEntity.ok(configItem);
}
}
class ConfigItem {
private final String value; // Problematic field
public String getKey() { return "example"; }
public String getValue() { return value; }
}