Java Programming
Java OOP, collections, multithreading
958 Questions 10 Topics Take Test
Advertisement
Showing 31–40 of 958 questions
How does Spring handle circular dependency between two beans?
A Automatically resolves it using setter injection for one of the beans
B Throws BeanCurrentlyInCreationException at startup
C Silently ignores one of the dependencies
D Uses a proxy pattern to defer initialization of one bean
Correct Answer:  A. Automatically resolves it using setter injection for one of the beans
EXPLANATION

Spring can resolve circular dependencies when at least one injection is done via setter method rather than constructor, allowing lazy initialization.

Take Test
What is the primary advantage of using Spring Dependency Injection over manual object creation?
A Reduces code coupling, improves testability, and simplifies bean management
B Increases application performance by caching objects in memory
C Eliminates the need for writing Java code
D Automatically implements all design patterns used in the application
Correct Answer:  A. Reduces code coupling, improves testability, and simplifies bean management
EXPLANATION

DI reduces tight coupling between classes, making code more maintainable, testable, and flexible for changes or mock implementations during testing.

Take Test
In Spring Data JPA, what does the @Repository annotation signify?
A It marks a class as a data access object and enables exception translation
B It automatically generates SQL queries from method names
C It configures the database connection pool
D It enables caching for database queries
Correct Answer:  A. It marks a class as a data access object and enables exception translation
EXPLANATION

@Repository marks a class as a data access component and enables Spring to translate persistence-specific exceptions into Spring's DataAccessException hierarchy.

Take Test
What is the role of PropertySource annotation in Spring Framework?
A To define external property files and load their values into the environment
B To create new properties dynamically at runtime
C To encrypt sensitive properties automatically
D To validate property values against defined constraints
Correct Answer:  A. To define external property files and load their values into the environment
EXPLANATION

@PropertySource allows loading properties from external files (like .properties or .yml) into the Spring Environment for use throughout the application.

Take Test
Consider a scenario where you have two bean implementations of the same interface. How would you specify which one to inject using Spring annotations?
A Use @Primary on one bean and @Autowired on the injection point
B Use @Qualifier annotation with the bean name at the injection point
C Use @Autowired with @Primary or @Qualifier; both approaches work
D Use @Bean with explicit configuration in @Configuration class
Correct Answer:  C. Use @Autowired with @Primary or @Qualifier; both approaches work
EXPLANATION

Both @Primary (default choice) and @Qualifier (explicit selection by name) can resolve bean ambiguity. @Qualifier is more explicit and takes precedence over @Primary.

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

Take Test
What is the difference between @Component and @Service annotations in Spring?
A @Component is generic, while @Service is semantically used for business logic classes
B @Service provides additional database transaction management capabilities
C @Component requires manual bean registration, while @Service auto-registers
D There is no functional difference; they are completely interchangeable
Correct Answer:  A. @Component is generic, while @Service is semantically used for business logic classes
EXPLANATION

@Component is a generic stereotype, while @Service is a specialized annotation indicating the class contains business logic, improving code readability.

Take Test
Which of the following correctly describes the lifecycle of a singleton-scoped Spring bean?
A Created once per HTTP request and destroyed after the request completes
B Created once when the ApplicationContext is initialized and persists throughout the application lifetime
C Created every time it is referenced and destroyed immediately after use
D Created in a separate thread context and shared across multiple threads
Correct Answer:  B. Created once when the ApplicationContext is initialized and persists throughout the application lifetime
EXPLANATION

Singleton beans are instantiated once per Spring container and reused for the application's lifetime, making them memory-efficient for stateless objects.

Take Test
In Spring Framework, what is the primary purpose of the @Autowired annotation?
A To automatically inject dependent beans into a class
B To define a new bean in the Spring container
C To configure database connections
D To enable component scanning
Correct Answer:  A. To automatically inject dependent beans into a class
EXPLANATION

@Autowired is used for dependency injection, allowing Spring to automatically resolve and inject bean dependencies without explicit configuration.

Take Test
A microservices architecture uses Spring with circular dependency between ServiceA and ServiceB. What is the best solution?
A Increase heap memory to handle circular references
B Refactor to introduce a third service or use setter injection with @Lazy
C Use @Autowired on both constructor and setter
D Switch to manual bean management
Correct Answer:  B. Refactor to introduce a third service or use setter injection with @Lazy
EXPLANATION

Circular dependencies indicate design issues. Best solutions: refactor code, use setter injection (instead of constructor) combined with @Lazy, or introduce an intermediary service to break the cycle.

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