Dependency Injection interview questions
Constructor vs Field Injection
26 questions with answers and explanations · 25 flashcards
Questions
- Why might you prefer constructor injection over field injection when unit testing?Multiple choiceMedium
- In Spring Boot, how can you ensure that a bean is eagerly initialized at startup?Multiple choiceHard
- What advantage does constructor injection offer over field injection concerning encapsulation?Multiple choiceMedium
- In Spring Boot, how does lazy initialization of beans affect constructor injection?Multiple choiceHard
- How does Spring Boot handle dependency injection when multiple beans match the type being injected?Multiple choiceMedium
- Which scope of a bean in Spring Boot is the default in Java?Multiple choiceEasy
- Why do teams use the @Autowired annotation in Spring Boot in production code?Multiple choiceEasy
- Why is constructor injection preferred over field injection in terms of immutability?Multiple choiceMedium
- Why is field injection a design problem?Multiple choiceMedium
- In Spring Boot, what is the effect of marking a bean as @Primary?Multiple choiceMedium
- How does using 'final' with constructor-injected dependencies in Spring Boot help during development?Multiple choiceMedium
- In the context of Spring Boot, what is a potential downside of self-invocation in transaction management?Multiple choiceHard
- What role does the Spring IoC container play in dependency injection?Multiple choiceMedium
- In Spring Boot, what role does the ApplicationContext play in dependency injection?Multiple choiceMedium
- How would you explain a key advantage of using Spring Boot over traditional Java applications for dependency injection in an interview?Multiple choiceEasy
- What issue might arise from using self-invocation in a transactional method within a Spring Boot application?Multiple choiceHard
- Why can using field injection with private fields in a Spring Boot application become a problem in practice?Multiple choiceEasy
- Why is constructor injection preferred for mandatory dependencies?Multiple choiceMedium
- How would you explain a key benefit of using constructor injection with 'final' fields in Spring Boot in an interview?Multiple choiceEasy
- Why is constructor injection generally preferred over field injection in Spring Boot applications?Multiple choiceEasy
- What issue might arise with field injection using @Autowired on private fields in a Spring Boot application?Multiple choiceMedium
- What impact does using constructor injection have on unit testing in Spring Boot applications?Multiple choiceHard
- Which of the following is a benefit of using 'final' with constructor-injected dependencies?Multiple choiceMedium
- Why is using @Autowired on constructor parameters recommended in Spring Boot?Multiple choiceEasy
- In Spring Boot, what is a potential problem when using field injection with the @Autowired annotation?Multiple choiceMedium
- How does Spring Boot handle bean instantiation order when using constructor injection?Multiple choiceHard