Kotlin Kotlin Android Development 2 — Questions and Answers
Question 1: What is `remember` used for in Jetpack Compose?
- Storing data in a database
- Preserving a value across recompositions (Correct answer)
- Creating animations
- Defining navigation routes
Correct answer: Preserving a value across recompositions
`remember` stores a value in composition memory so it survives recompositions, but is reset if the composable leaves the composition.
Question 2: What is `mutableStateOf` in Jetpack Compose?
- A way to create a mutable list
- A state holder that triggers recomposition when its value changes (Correct answer)
- A coroutine state machine
- A thread-safe map implementation
Correct answer: A state holder that triggers recomposition when its value changes
`mutableStateOf` creates an observable state object; when its value changes, Compose schedules a recomposition of composables reading the state.
Question 3: Which Kotlin library is commonly used for dependency injection in Android apps?
- Retrofit
- Hilt (Correct answer)
- OkHttp
- Gson
Correct answer: Hilt
Hilt is the recommended dependency injection library for Android, built on top of Dagger and integrated with Android lifecycle components.
Question 4: What is Retrofit used for in Android Kotlin development?
- Database migrations
- A type-safe HTTP client for making network requests (Correct answer)
- UI layout inflation
- Thread management
Correct answer: A type-safe HTTP client for making network requests
Retrofit is a type-safe HTTP client for Android that converts REST API endpoints into Kotlin interface methods.
Question 5: What does `LaunchedEffect` do in Jetpack Compose?
- Launches a new Activity
- Runs a suspend function in composition with a given key, relaunching when the key changes (Correct answer)
- Creates a launch animation
- Handles back press events
Correct answer: Runs a suspend function in composition with a given key, relaunching when the key changes
`LaunchedEffect` runs a coroutine block tied to the composition lifecycle, cancelling and relaunching it when the specified key changes.
Question 6: What is `StateFlow` commonly used for in Android ViewModel?
- Storing media files
- Exposing observable UI state from ViewModel to Composables or Fragments (Correct answer)
- Managing network timeouts
- Handling back stack navigation
Correct answer: Exposing observable UI state from ViewModel to Composables or Fragments
`StateFlow` in ViewModel exposes a hot stream of UI state that Compose or Fragment collectors can observe reactively.
What is `remember` used for in Jetpack Compose?