0
0
Spring Bootframework~20 mins

IoC container mental model in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
IoC Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when Spring Boot starts the IoC container?

When a Spring Boot application starts, what does the IoC container do first?

AIt deletes all existing beans and starts with an empty container.
BIt scans for classes annotated with @Component, @Service, @Repository, and creates their instances as beans.
CIt waits for user input before loading any beans.
DIt immediately runs all @PostConstruct methods before creating any beans.
Attempts:
2 left
💡 Hint

Think about how Spring finds and prepares the parts of your app it manages.

state_output
intermediate
2:00remaining
What is the state of a bean after Spring IoC container finishes initialization?

After the Spring IoC container finishes initializing, what is true about the beans it manages?

ABeans are created but their lifecycle methods like @PostConstruct are not called.
BBeans are only created when first requested by the application code.
CBeans exist but have no dependencies injected yet.
DAll singleton beans are created and ready to use, with dependencies injected.
Attempts:
2 left
💡 Hint

Consider the default scope of beans and when dependencies are set.

📝 Syntax
advanced
2:00remaining
Which code snippet correctly defines a Spring bean with constructor injection?

Choose the correct way to define a Spring bean using constructor injection in a @Component class.

A
@Component
public class ServiceA {
  private final Repo repo;
  public ServiceA(Repo repo) { this.repo = repo; }
}
B
@Component
public class ServiceA {
  @Autowired
  private Repo repo;
  public ServiceA() {}
}
C
@Component
public class ServiceA {
  private Repo repo;
  public void setRepo(Repo repo) { this.repo = repo; }
}
D
@Component
public class ServiceA {
  private Repo repo;
  public ServiceA() { repo = new Repo(); }
}
Attempts:
2 left
💡 Hint

Constructor injection means passing dependencies through the constructor parameters.

🔧 Debug
advanced
2:00remaining
Why does this Spring bean fail to initialize?

Given this Spring bean code, why does the application fail to start?

@Component
public class MyService {
  private final MyRepository repo;
  public MyService() {}
}
ABecause the constructor does not accept MyRepository, Spring cannot inject the dependency.
BBecause @Component is missing, Spring does not detect the class.
CBecause MyRepository is not annotated with @Service, injection fails.
DBecause the class has no @Autowired annotation on the constructor.
Attempts:
2 left
💡 Hint

Think about how Spring injects dependencies when constructors are present.

🧠 Conceptual
expert
3:00remaining
How does Spring IoC container manage bean lifecycle callbacks?

Which statement best describes how the Spring IoC container manages lifecycle callbacks like @PostConstruct and DisposableBean?

ASpring ignores lifecycle annotations and requires manual calls to lifecycle methods.
BSpring calls @PostConstruct methods before creating the bean instance, and DisposableBean methods immediately after creation.
CSpring calls @PostConstruct methods after creating and injecting dependencies, and calls DisposableBean methods before destroying singleton beans.
DSpring only calls lifecycle methods if the bean is prototype scoped.
Attempts:
2 left
💡 Hint

Think about when initialization and destruction happen in the bean lifecycle.