0
0
Spring Bootframework~20 mins

Constructor injection (preferred) in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Constructor Injection Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Spring Boot component using constructor injection?

Consider this Spring Boot service class using constructor injection. What will be printed when greet() is called?

Spring Boot
import org.springframework.stereotype.Service;

@Service
public class GreetingService {
    private final String message;

    public GreetingService() {
        this.message = "Hello, World!";
    }

    public String greet() {
        return message;
    }
}
Anull
B"Hello, World!"
CCompilation error due to missing constructor injection
D"Hello"
Attempts:
2 left
💡 Hint

Look at how the message field is set in the constructor.

📝 Syntax
intermediate
2:00remaining
Which constructor injection code snippet is syntactically correct in Spring Boot?

Choose the correct constructor injection syntax for a Spring Boot service that depends on DataService.

Apublic MyService(DataService dataService) { dataService = dataService; }
Bpublic MyService() { this.dataService = new DataService(); }
Cpublic void MyService(DataService dataService) { this.dataService = dataService; }
Dpublic MyService(DataService dataService) { this.dataService = dataService; }
Attempts:
2 left
💡 Hint

Remember constructors have no return type and parameters are assigned to fields.

🔧 Debug
advanced
2:00remaining
Why does this Spring Boot component fail to inject dependency via constructor?

Examine this Spring Boot component. Why will the dependency injection fail?

Spring Boot
import org.springframework.stereotype.Component;

@Component
public class UserComponent {
    private final UserService userService;

    public UserComponent() {
        this.userService = null;
    }

    public String getUserName() {
        return userService.getName();
    }
}
ABecause @Component annotation is missing
BBecause userService is final and cannot be null
CBecause the constructor does not accept UserService, so Spring cannot inject it
DBecause getUserName() method is not annotated with @Autowired
Attempts:
2 left
💡 Hint

Think about how Spring injects dependencies via constructors.

state_output
advanced
2:00remaining
What is the value of the injected field after Spring Boot creates this component?

Given this Spring Boot component using constructor injection, what will be the value of configValue after creation?

Spring Boot
import org.springframework.stereotype.Component;

@Component
public class ConfigComponent {
    private final String configValue;

    public ConfigComponent(String configValue) {
        this.configValue = configValue;
    }

    public String getConfigValue() {
        return configValue;
    }
}
ACompilation error due to missing @Value annotation
Bnull
CEmpty string ""
DThe string value from Spring configuration if properly wired
Attempts:
2 left
💡 Hint

Think about how Spring injects simple types like String in constructors.

🧠 Conceptual
expert
2:00remaining
Why is constructor injection preferred over field injection in Spring Boot?

Choose the best explanation why constructor injection is preferred in Spring Boot applications.

AConstructor injection ensures dependencies are immutable and required, improving testability and design clarity
BConstructor injection automatically wires private fields without setters
CConstructor injection requires less code and no explicit annotations
DConstructor injection allows Spring to inject dependencies lazily at runtime
Attempts:
2 left
💡 Hint

Think about immutability and testing benefits.