Consider the following code:
List<String> fruits = Arrays.asList("apple", "banana", "cherry");
fruits.removeIf(s -> s.length() > 5);
What will be the contents of 'fruits' list after execution?
Answer: B
removeIf() removes all elements that satisfy the lambda predicate. Strings with length > 5 are "banana" (6 chars) and "cherry" (6 chars). Only "apple" (5 chars) remains as its length is not greater than 5.
Q.442Medium
A Spring application requires constructor-based dependency injection for a mandatory dependency. Which approach is best?
Answer: B
Constructor-based DI ensures mandatory dependencies are provided at object creation time and is considered a best practice for required dependencies.
Q.443Medium
What is the default scope of a Spring Bean?
Answer: C
By default, Spring creates beans as singletons - one instance per application context, shared across the entire application.
Q.444Medium
Which of the following best describes IoC (Inversion of Control) in Spring?
Answer: A
IoC is a principle where the Spring Container manages object creation and lifecycle, not the application code itself.
Q.445Medium
A developer needs to inject a bean only if it exists, otherwise skip injection. Which annotation should be used?
Answer: B
@Autowired(required=false) allows optional dependency injection. If the bean doesn't exist, no error is thrown.
Advertisement
Q.446Medium
What is the role of ApplicationContext in Spring Framework?
Answer: B
ApplicationContext is the central Spring container that manages bean lifecycle, performs DI, and provides additional features like event publishing and message resolution.
Q.447Medium
Which annotation is used to define configuration classes in Spring that replace XML configuration?
Answer: C
@Configuration marks a class as a source of bean definitions. It's equivalent to an XML <beans> element and works with @Bean methods.
Q.448Medium
Consider a scenario where multiple beans of the same type exist. How can you specify which bean to inject?
Answer: B
@Qualifier specifies the bean name to inject when multiple candidates exist. @Primary can also be used to mark a preferred bean.
Q.449Medium
What is the difference between @Bean and @Component annotations?
Answer: C
@Component marks a class as a bean, while @Bean is used inside @Configuration classes to define beans through factory methods, providing more control.
Q.450Medium
A Spring Boot application starts with ClassPathXmlApplicationContext('application.xml'). What does this do?
Answer: A
ClassPathXmlApplicationContext loads Spring bean definitions from an XML file located in the classpath, allowing traditional XML-based configuration.
Q.451Medium
Which Spring concept ensures that the same bean instance is reused throughout the application lifecycle?
Answer: C
Singleton scope (default) creates one bean instance per application context that is shared and reused across the entire application.
Q.452Medium
In a Spring Boot application, what does the @SpringBootApplication annotation combine?
Answer: A
@SpringBootApplication is a convenience annotation that combines three key annotations for configuration, component scanning, and auto-configuration.
Q.453Medium
Consider a scenario where you have two bean implementations of the same interface. How would you specify which one to inject using Spring annotations?
Answer: C
Both @Primary (default choice) and @Qualifier (explicit selection by name) can resolve bean ambiguity. @Qualifier is more explicit and takes precedence over @Primary.
Q.454Medium
What is the role of PropertySource annotation in Spring Framework?
Answer: A
@PropertySource allows loading properties from external files (like .properties or .yml) into the Spring Environment for use throughout the application.
Q.455Medium
In Spring Data JPA, what does the @Repository annotation signify?
Answer: A
@Repository marks a class as a data access component and enables Spring to translate persistence-specific exceptions into Spring's DataAccessException hierarchy.
Q.456Medium
What is the function of the BeanFactory interface in Spring?
Answer: A
BeanFactory is the core interface for accessing beans, providing lazy initialization. ApplicationContext extends BeanFactory with additional enterprise features.
Q.457Medium
In Spring AOP, what does the term 'Join Point' refer to?
Answer: A
A Join Point is a candidate point in program execution (method call, exception throw) where advice can be applied. Pointcuts select specific Join Points.
Q.458Medium
What does the @Transactional annotation do in Spring?
Answer: A
@Transactional delegates transaction management to Spring, automatically committing on success and rolling back on exceptions, following ACID principles.
Q.459Medium
What is the significance of the @ConfigurationProperties annotation in Spring Boot?
Answer: A
@ConfigurationProperties enables binding of external properties (from application.properties/yml) to POJO classes, providing type safety and validation.
Q.460Medium
What does the @EnableCaching annotation enable in a Spring application?
Answer: A
@EnableCaching activates Spring's caching infrastructure, allowing method-level caching through annotations like @Cacheable to store and retrieve results.