🌱 Spring Boot MCQ Questions – Page 49
Questions 961–971 of 971 total — Spring Boot interview practice
▶ Practice All Spring Boot QuestionsWhen a method annotated with `@GetMapping` inside a `@Controller` returns a `String` (e.g., `"products/list"`), what does Spring Boot typically do with this string?
What is the HTTP response body when a GET request is made to `/config/theme/dark`?
java
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController
public class ConfigController {
@GetMapping("/config/{key}/{value}")
public String setConfig(@PathVariable Map<String, String> pathVars) {
return "Config set: " + pathVars.get("key") + " = " + pathVars.get("value");
}
}
Which HTTP status code is most appropriate to return from a Spring Boot DELETE endpoint when a resource has been successfully deleted and there is no content to send in the response body?
What is the expected behavior if a Spring Boot controller returns a view name that includes a path like `/templates/home` instead of just `home`, assuming Thymeleaf is configured by default and `home.html` is in `src/main/resources/templates/`?
java
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.ui.Model;
@Controller
public class HomeController {
@GetMapping("/home")
public String displayHome(Model model) {
model.addAttribute("greeting", "Welcome!");
return "/templates/home"; // Incorrect path prefix
}
}
Given the following Spring configuration, what error will be thrown during application context initialization?
java
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
@Bean(name = "mySharedBean")
public String beanA() {
return "Value A";
}
@Bean(name = "mySharedBean")
public Integer beanB() {
return 123;
}
}
How can you inject a simple string property named `app.name` from `application.properties` into a Spring component field?
What is the consequence of setting `required = false` in `@Autowired`, e.g., `@Autowired(required = false)`?
What does this code print?
java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
interface NotificationService { String send(); }
@Component("emailService") static class EmailNotificationService implements NotificationService { @Override public String send() { return "Sending Email"; } }
@Component("smsService") static class SmsNotificationService implements NotificationService { @Override public String send() { return "Sending SMS"; } }
@SpringBootApplication
public class MyApplication {
@Autowired @Qualifier("smsService")
private NotificationService notificationService;
public static void main(String[] args) {
MyApplication app = SpringApplication.run(MyApplication.class, args).getBean(MyApplication.class);
System.out.println(app.notificationService.send());
}
}
Consider the following Spring Boot utility class for JWT. What will be printed when parsing a token signed with a *different* secret key?
java
import io.jsonwebtoken.*;
import io.jsonwebtoken.security.Keys;
import java.security.Key;
import java.util.Date;
public class JwtUtil {
public static void main(String[] args) {
Key correctKey = Keys.secretKeyFor(SignatureAlgorithm.HS256);
Key wrongKey = Keys.secretKeyFor(SignatureAlgorithm.HS256);
String token = Jwts.builder().setSubject("user").signWith(correctKey).compact();
try {
Jwts.parserBuilder().setSigningKey(wrongKey).build().parseClaimsJws(token);
System.out.println("Token is valid");
} catch (SignatureException e) {
System.out.println("SignatureException caught");
} catch (Exception e) {
System.out.println("Other exception: " + e.getClass().getSimpleName());
}
}
}
Can a single Spring Boot controller method typically have multiple parameters annotated with `@RequestBody`?
What is the result when this Spring Boot application attempts to start?
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;
interface GreetingService {
String greet();
}
@Component
class EnglishGreetingService implements GreetingService {
@Override public String greet() { return "Hello!"; }
}
@Component
class SpanishGreetingService implements GreetingService {
@Override public String greet() { return "Hola!"; }
}
@Component
class MyRunner implements CommandLineRunner {
@Autowired
private GreetingService service;
@Override
public void run(String... args) {
System.out.println(service.greet());
}
}
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}