0
0
Spring Bootframework~20 mins

@Autowired for dependency injection in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Spring Boot Dependency Injection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
What does @Autowired do in Spring Boot?
In Spring Boot, what is the primary purpose of the @Autowired annotation?
AIt marks a class as a Spring Boot application entry point.
BIt automatically injects the required dependency into a class field or constructor.
CIt defines a REST endpoint in a controller class.
DIt configures database connection properties.
Attempts:
2 left
💡 Hint
Think about how Spring Boot provides objects to classes without manual creation.
component_behavior
intermediate
2:00remaining
What happens if Spring cannot find a bean to @Autowired?
Consider a Spring Boot class with a field annotated with @Autowired. What happens if Spring cannot find a matching bean to inject?
ASpring creates a new bean automatically without any error.
BSpring logs a warning but continues running.
CThe field remains null and the application runs without errors.
DThe application fails to start with a NoSuchBeanDefinitionException.
Attempts:
2 left
💡 Hint
Think about what Spring expects when it cannot find a required dependency.
📝 Syntax
advanced
2:00remaining
Which code snippet correctly uses @Autowired for constructor injection?
Select the code snippet that correctly uses @Autowired to inject a dependency via constructor in a Spring Boot component.
A
public class Service {
  private final Repo repo;
  @Autowired
  public Service(Repo repo) {
    this.repo = repo;
  }
}
B
public class Service {
  @Autowired
  private Repo repo;
  public Service() {}
}
C
public class Service {
  private Repo repo;
  public Service(@Autowired Repo repo) {
    this.repo = repo;
  }
}
D
public class Service {
  private Repo repo;
  public Service(Repo repo) {
    this.repo = repo;
  }
}
Attempts:
2 left
💡 Hint
Look for the correct placement of @Autowired on the constructor.
🔧 Debug
advanced
2:00remaining
Why does this @Autowired field remain null?
Given this Spring Boot component code, why does the service field remain null? public class Controller { @Autowired private Service service; public void doWork() { service.perform(); } } Assume Service is a valid Spring bean.
AThe Controller class is not managed by Spring (missing @Component or similar).
BThe Service bean is not annotated with @Service.
CThe @Autowired annotation is missing on the service field.
DThe perform() method does not exist in Service.
Attempts:
2 left
💡 Hint
Think about which classes Spring manages and injects dependencies into.
lifecycle
expert
2:00remaining
When is the @Autowired injection performed in Spring Boot lifecycle?
At what point in the Spring Boot application lifecycle does the @Autowired injection happen for a bean?
AAfter the application context is fully started and running.
BBefore the constructor is called, during class loading.
CDuring bean creation, after the constructor but before any @PostConstruct methods.
DOnly when the bean is first accessed by another bean.
Attempts:
2 left
💡 Hint
Consider when Spring sets dependencies relative to constructor and lifecycle callbacks.