0
0
Android Kotlinmobile~20 mins

Dependency injection with Hilt in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Hilt Dependency Injection Master
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 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)
AHello from Repository
Bnull
CCompilation error due to missing @Inject on ViewModel
DRuntime crash due to missing Hilt setup
Attempts:
2 left
💡 Hint
Remember that Hilt provides the dependencies automatically when properly set up.
📝 Syntax
intermediate
2: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()
    }
}
A
@Provides
fun provideRepository(): MyRepository = MyRepository()
B
@Provides
@Singleton
fun provideRepository(): MyRepository = MyRepository()
C
@Singleton
fun provideRepository(): MyRepository = MyRepository()
D
@Provides
@Singleton
fun provideRepository(): MyRepository { MyRepository() }
Attempts:
2 left
💡 Hint
Look for correct annotations and function syntax.
🔧 Debug
advanced
2: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 }
Aerror: @Inject constructor is missing for UserRepository
Berror: Duplicate bindings found for UserRepository
Cerror: Cannot find symbol UserRepository
DNo error, Hilt will create the instance automatically
Attempts:
2 left
💡 Hint
Hilt requires @Inject on constructors to know how to create instances.
state_output
advanced
2: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
}
AOne instance for the entire app lifecycle
BA new instance every time it is injected
COne instance per activity instance
DOne instance per fragment instance
Attempts:
2 left
💡 Hint
Think about what 'ActivityScoped' means in terms of lifecycle.
🧠 Conceptual
expert
2: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?
AFragmentComponent > ActivityComponent > SingletonComponent > ViewComponent
BActivityComponent > SingletonComponent > FragmentComponent > ViewComponent
CViewComponent > FragmentComponent > ActivityComponent > SingletonComponent
DSingletonComponent > ActivityComponent > FragmentComponent > ViewComponent
Attempts:
2 left
💡 Hint
Consider which component lives the longest in the app.