Showing 21–30 of 476 questions
In a Spring Boot application, what does the @SpringBootApplication annotation combine?
A
@Configuration, @ComponentScan, and @EnableAutoConfiguration
B
@Bean, @Autowired, and @Primary
C
@RestController, @RequestMapping, and @PostMapping
D
@Repository, @Service, and @Component
Correct Answer:
A. @Configuration, @ComponentScan, and @EnableAutoConfiguration
EXPLANATION
@SpringBootApplication is a convenience annotation that combines three key annotations for configuration, component scanning, and auto-configuration.
Which Spring concept ensures that the same bean instance is reused throughout the application lifecycle?
A
Prototype scope
B
Request scope
C
Singleton scope
D
Session scope
Correct Answer:
C. Singleton scope
EXPLANATION
Singleton scope (default) creates one bean instance per application context that is shared and reused across the entire application.
A Spring Boot application starts with ClassPathXmlApplicationContext('application.xml'). What does this do?
A
Creates an ApplicationContext and loads bean definitions from XML file on classpath
B
Scans all @Component classes automatically
C
Initializes Spring Security
D
Creates embedded Tomcat server
Correct Answer:
A. Creates an ApplicationContext and loads bean definitions from XML file on classpath
EXPLANATION
ClassPathXmlApplicationContext loads Spring bean definitions from an XML file located in the classpath, allowing traditional XML-based configuration.
What is the difference between @Bean and @Component annotations?
A
They are identical and interchangeable
B
@Bean is on class level, @Component is on method level
C
@Component is on class level, @Bean is on method level inside @Configuration classes
D
@Bean only works with XML configuration
Correct Answer:
C. @Component is on class level, @Bean is on method level inside @Configuration classes
EXPLANATION
@Component marks a class as a bean, while @Bean is used inside @Configuration classes to define beans through factory methods, providing more control.
Consider a scenario where multiple beans of the same type exist. How can you specify which bean to inject?
A
Use @Autowired alone
B
Use @Qualifier annotation with bean name
C
Use @Primary and @Qualifier together
D
Delete all but one bean
Correct Answer:
B. Use @Qualifier annotation with bean name
EXPLANATION
@Qualifier specifies the bean name to inject when multiple candidates exist. @Primary can also be used to mark a preferred bean.
Which annotation is used to define configuration classes in Spring that replace XML configuration?
A
@ConfigClass
B
@SpringConfig
C
@Configuration
D
@Bean
Correct Answer:
C. @Configuration
EXPLANATION
@Configuration marks a class as a source of bean definitions. It's equivalent to an XML <beans> element and works with @Bean methods.
What is the role of ApplicationContext in Spring Framework?
A
To execute SQL queries
B
To manage beans, enable dependency injection, and provide application-wide features
C
To compile Java code
D
To generate REST endpoints automatically
Correct Answer:
B. To manage beans, enable dependency injection, and provide application-wide features
EXPLANATION
ApplicationContext is the central Spring container that manages bean lifecycle, performs DI, and provides additional features like event publishing and message resolution.
A developer needs to inject a bean only if it exists, otherwise skip injection. Which annotation should be used?
A
@Required
B
@Autowired(required=false)
C
@Nullable
D
@Optional
Correct Answer:
B. @Autowired(required=false)
EXPLANATION
@Autowired(required=false) allows optional dependency injection. If the bean doesn't exist, no error is thrown.
Which of the following best describes IoC (Inversion of Control) in Spring?
A
Control of object creation is transferred from application to Spring Container
B
Application controls all object creation
C
Objects cannot be created dynamically
D
Spring eliminates the need for object-oriented programming
Correct Answer:
A. Control of object creation is transferred from application to Spring Container
EXPLANATION
IoC is a principle where the Spring Container manages object creation and lifecycle, not the application code itself.
What is the default scope of a Spring Bean?
A
prototype
B
request
C
singleton
D
session
Correct Answer:
C. singleton
EXPLANATION
By default, Spring creates beans as singletons - one instance per application context, shared across the entire application.