Challenge - 5 Problems
Hilt Dependency Injection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Hilt-injected ViewModel initialization?
Consider a ViewModel injected with Hilt in an Android app. What will be the output when accessing the injected repository's data property?
Android Kotlin
class MyRepository @Inject constructor() { val data = "Hello from Repository" } @HiltViewModel class MyViewModel @Inject constructor( val repository: MyRepository ) : ViewModel() // In an Activity or Fragment val viewModel: MyViewModel by viewModels() println(viewModel.repository.data)
Attempts:
2 left
💡 Hint
Remember that Hilt provides the dependencies automatically when properly set up.
✗ Incorrect
The repository is injected into the ViewModel by Hilt, so accessing repository.data returns the string defined in the repository.
📝 Syntax
intermediate2:00remaining
Which option correctly provides a singleton repository with Hilt?
You want to provide a singleton instance of MyRepository using Hilt. Which code snippet correctly achieves this?
Android Kotlin
class MyRepository @Inject constructor() { // Repository implementation } @Module @InstallIn(SingletonComponent::class) object RepositoryModule { @Provides @Singleton fun provideRepository(): MyRepository { return MyRepository() } }
Attempts:
2 left
💡 Hint
Look for correct annotations and function syntax.
✗ Incorrect
Option B correctly uses @Provides and @Singleton annotations and returns the repository instance properly.
🔧 Debug
advanced2:00remaining
What error occurs when a Hilt-injected constructor is missing @Inject?
Given this class without @Inject on the constructor, what error will Hilt produce during compilation?
class UserRepository {
// No @Inject constructor
}
Attempts:
2 left
💡 Hint
Hilt requires @Inject on constructors to know how to create instances.
✗ Incorrect
Without @Inject on the constructor, Hilt cannot generate the code to provide UserRepository, causing a compilation error.
❓ state_output
advanced2:00remaining
What is the scope of a dependency annotated with @ActivityScoped in Hilt?
If a repository is annotated with @ActivityScoped, how many instances will exist during the app's lifecycle?
Android Kotlin
@ActivityScoped class ScopedRepository @Inject constructor() { var count = 0 }
Attempts:
2 left
💡 Hint
Think about what 'ActivityScoped' means in terms of lifecycle.
✗ Incorrect
@ActivityScoped means the same instance is shared within one activity but different activities get different instances.
🧠 Conceptual
expert2:00remaining
Which statement best describes Hilt's component hierarchy?
Hilt uses a component hierarchy to manage dependency scopes. Which option correctly orders the components from the longest to shortest lifecycle?
Attempts:
2 left
💡 Hint
Consider which component lives the longest in the app.
✗ Incorrect
SingletonComponent lives for the entire app, ActivityComponent for each activity, FragmentComponent for each fragment, and ViewComponent for each view.