Java Programming
Java OOP, collections, multithreading
958 Questions 10 Topics Take Test
Advertisement
Showing 41–50 of 958 questions
In Spring Framework 2024-25, which feature allows lazy initialization of beans?
A @Lazy annotation on @Bean or @Component
B @DeferredInit annotation
C Setting lazy-init='true' only in XML
D Spring automatically detects and lazily initializes all beans
Correct Answer:  A. @Lazy annotation on @Bean or @Component
EXPLANATION

@Lazy annotation (available in modern Spring versions) marks beans for lazy initialization. It can be used on class level or with @Bean methods. XML also supports lazy-init attribute.

Take Test
What happens when a prototype-scoped bean has a dependency on a singleton-scoped bean in Spring?
A Compilation error occurs
B The singleton bean is correctly injected into each prototype instance
C The prototype bean becomes singleton
D A runtime exception is thrown
Correct Answer:  B. The singleton bean is correctly injected into each prototype instance
EXPLANATION

A prototype bean can depend on singleton beans without issues. Each prototype instance gets the same singleton instance injected. The reverse (singleton depending on prototype) is problematic.

Take Test
An enterprise application needs different bean implementations based on environment (dev/prod). Which approach is most suitable?
A @Bean with conditional logic inside @Configuration
B @Conditional annotation with custom conditions
C @Profile annotation to define environment-specific beans
D Manual bean creation in main() method
Correct Answer:  C. @Profile annotation to define environment-specific beans
EXPLANATION

@Profile allows defining beans for specific environments (dev, prod, test). Alternatively, @Conditional provides more granular control with custom conditions.

Take Test
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.

Take Test
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.

Take Test
Advertisement
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.

Take Test
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.

Take Test
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.

Take Test
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.

Take Test
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.

Take Test
IGET
iget AI
Online · Ask anything about exams
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips