🌱 Spring Boot MCQ Questions – Page 29
Questions 561–580 of 971 total — Spring Boot interview practice
▶ Practice All Spring Boot QuestionsWhich of the following describes the most common use case for `@RequestParam` in a Spring Boot controller?
What is the result when a GET request is made to `/api/process` with the following controller setup?
java
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Service;
@Service
class MyService { public String doSomething() { return "Processed!"; } }
@RestController
public class MyController {
private MyService myService; // Missing @Autowired
@GetMapping("/api/process")
public String processData() {
return myService.doSomething();
}
}
What is the output of this code?
java
package com.example.repository; // Different package
import org.springframework.stereotype.Repository;
@Repository
class ProductRepository {
public ProductRepository() {
System.out.println("ProductRepository initialized.");
}
}
package com.example.app; // Main application package
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
@SpringBootApplication(scanBasePackages = "com.example.app") // Explicitly scanning wrong package
public class MainApplication {
public static void main(String[] args) {
try {
ConfigurableApplicationContext context = SpringApplication.run(MainApplication.class, args);
context.getBean(com.example.repository.ProductRepository.class); // Fails here
context.close();
} catch (NoSuchBeanDefinitionException e) {
System.out.println("Caught exception: " + e.getClass().getSimpleName());
}
}
}
Assume `MyCustomBean` is a simple class with a `getValue()` method. An `@Configuration` class (`AppConfig`) provides a `@Bean` for `MyCustomBean` initialized with "Hello from AppConfig Bean". What does this code print to the console when `testCustomBeanInjection` is executed?
java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.junit.jupiter.api.Test;
// MyCustomBean and AppConfig (not shown) with an @Bean method for MyCustomBean.
@SpringBootTest
public class CustomBeanTest {
@Autowired
private MyCustomBean myCustomBean;
@Test
void testCustomBeanInjection() {
System.out.println(myCustomBean.getValue());
}
}
What error occurs when accessing `/static/message` with the following controller?
java
import org.springframework.web.bind.annotation.*;
@RestController
public class StaticController {
@GetMapping("/static/message")
public String getStaticMessage(@PathVariable String value) {
return "Message: " + value;
}
}
Consider the following service. What error, if any, will occur if this `MyService` is part of a Spring Boot application?
java
package com.example.app;
import org.springframework.stereotype.Service;
@Service
final class MyService {
public String getValue() {
return "Final Service";
}
}
What is the output printed to the console when the following Spring Boot `@Service` (which is a singleton by default) is used multiple times in a `CommandLineRunner`? (Assume standard Spring Boot application setup and necessary imports.)
java
import org.springframework.stereotype.Service;
@Service
class CounterService {
private int count = 0;
public int incrementAndGet() { return ++count; }
}
// In a @SpringBootApplication's CommandLineRunner, after autowiring:
// CounterService counterServiceInstance;
// counterServiceInstance.incrementAndGet();
// System.out.println(counterServiceInstance.incrementAndGet());
What error will occur during Spring Boot application startup with the following configuration?
java
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.beans.factory.annotation.Qualifier;
public class MyComponent {}
@Configuration
public class AppConfig {
@Bean
public MyComponent myComponent(@Qualifier("nonExistentBean") Object param) {
return new MyComponent();
}
}
Which of the following injection types are supported by the `@Autowired` annotation?
What kind of error will occur when Spring Boot tries to initialize the following services?
java
package com.example.app;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
@Service
class ServiceA {
private final ServiceB serviceB;
@Autowired
public ServiceA(ServiceB serviceB) {
this.serviceB = serviceB;
}
}
@Service
class ServiceB {
private final ServiceA serviceA;
@Autowired
public ServiceB(ServiceA serviceA) {
this.serviceA = serviceA;
}
}
Given an `AppConfig` with two `String` beans named `profileMessage`, one `@Profile("dev")` returning "Dev Profile Active" and another `@Profile("prod")` returning "Prod Profile Active", what is the output of this test code?
java
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
@SpringBootTest
@ActiveProfiles("dev")
class ActiveProfilesTest {
@Autowired
String profileMessage;
@Test
void testActiveProfileBean() {
System.out.print(profileMessage);
}
}
Which `@Value` annotation correctly injects the result of the Spring Expression Language (SpEL) expression `10 * 5`?
Given the `spring.profiles.active` property is set to 'dev' (either in `application.properties` or as an environment variable) 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 ProfileInfo {
@Value("#{'Current profile is: ' + environment['spring.profiles.active']}")
private String profileMessage;
public String getProfileMessage() {
return profileMessage;
}
}
// In a Spring Boot application, if this component is scanned:
// ProfileInfo info = applicationContext.getBean(ProfileInfo.class);
// System.out.println(info.getProfileMessage());
Consider this Spring Boot controller method. What will happen when it is compiled?
java
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyController {
@GetMapping("/missing-return")
public ResponseEntity<String> testMethod() {
String message = "Hello";
// Missing return statement
}
}
Which of the following best describes the idempotency characteristic of a standard HTTP POST request used for resource creation?
What is the output of this Java code if an expired JWT is provided for parsing?
java
import io.jsonwebtoken.*;
import io.jsonwebtoken.security.Keys;
import java.security.Key;
import java.util.Date;
public class JwtParser {
public static void main(String[] args) {
Key key = Keys.secretKeyFor(SignatureAlgorithm.HS256);
// Token generated with expiration set to 1 second in the past
String expiredJwt = Jwts.builder()
.setSubject("user123")
.setExpiration(new Date(System.currentTimeMillis() - 1000L))
.signWith(key)
.compact();
try {
Jwts.parserBuilder().setSigningKey(key).build().parseClaimsJws(expiredJwt);
System.out.println("Token is valid");
} catch (ExpiredJwtException e) {
System.out.println("ExpiredJwtException caught");
} catch (Exception e) {
System.out.println("Other exception: " + e.getClass().getSimpleName());
}
}
}
What error will Spring Boot report when starting this application?
java
import org.springframework.stereotype.Service;
import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Autowired;
public interface NotificationService { String send(String msg); }
@Service class EmailService implements NotificationService {
@Override public String send(String msg) { return "Email"; }
}
@Service class SMSService implements NotificationService {
@Override public String send(String msg) { return "SMS"; }
}
@Component class MessageSender {
@Autowired private NotificationService notificationService;
}
What does the generic type parameter `<T>` in `ResponseEntity<T>` primarily represent?
What is the output of this code?
java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
interface Service { String name(); }
@Component @Primary
class PrimaryService implements Service { public String name() { return "Primary"; } }
@Component
class SecondaryService implements Service { public String name() { return "Secondary"; } }
@SpringBootApplication
public class App6 {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(App6.class, args);
Service service = ctx.getBean(Service.class);
System.out.println(service.name());
}
}
Which Spring MVC annotation is primarily used to bind incoming form data from HTML form fields to a domain object for processing in a controller method?