Challenge - 5 Problems
API Interface Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📝 Syntax
intermediate2:00remaining
Correct Kotlin interface syntax for API calls
Which option correctly defines a Kotlin interface method for a GET request to fetch a list of users using Retrofit?
Android Kotlin
interface ApiService {
@GET("users")
suspend fun getUsers(): List<User>
}Attempts:
2 left
💡 Hint
Remember Retrofit uses suspend functions for coroutines and the return type should match the expected data.
✗ Incorrect
Option A correctly uses suspend fun with the expected return type List. Option A misses suspend, so it won't work with coroutines. Option A returns Call
- > which is not a suspend function. Option A returns List
❓ ui_behavior
intermediate1:30remaining
API interface method effect on UI loading state
Given a Retrofit API interface method defined as a suspend function, what is the expected UI behavior when calling it inside a coroutine?
Attempts:
2 left
💡 Hint
Think about how suspend functions work with coroutines and UI responsiveness.
✗ Incorrect
Suspend functions in Retrofit run asynchronously without blocking the UI thread, so loading indicators can show while waiting. UI updates must be done on the main thread, but the call itself does not block UI.
❓ lifecycle
advanced2:00remaining
Handling API interface calls during Android lifecycle changes
What is the best practice to avoid memory leaks when calling a Retrofit API interface method inside an Android Activity that may be destroyed during the call?
Attempts:
2 left
💡 Hint
Consider how Android lifecycleScope helps manage coroutines tied to lifecycle.
✗ Incorrect
Using lifecycleScope ties the coroutine to the Activity lifecycle, automatically cancelling it if the Activity is destroyed, preventing memory leaks. GlobalScope is not lifecycle-aware and can cause leaks. Direct calls without coroutine or lifecycle awareness risk leaks.
advanced
1:30remaining
Navigating after successful API interface call
After a successful login API call defined in an interface, what is the correct way to navigate from LoginActivity to HomeActivity?
Android Kotlin
val response = apiService.loginUser(credentials)
if (response.isSuccessful) {
// Navigate to HomeActivity
}Attempts:
2 left
💡 Hint
Think about removing LoginActivity from back stack after navigation.
✗ Incorrect
Option C starts HomeActivity and finishes LoginActivity to prevent returning to login screen on back press. Option C does not finish LoginActivity, so back button returns there. Option C finishes before starting, which may cause errors. Option C uses incorrect method start() instead of startActivity().
🔧 Debug
expert2:30remaining
Identify the cause of runtime error in Retrofit API interface call
Given this Retrofit interface method:
interface ApiService {
@POST("submit")
suspend fun submitData(@Body data: DataModel): Response
}
And this call:
val response = apiService.submitData(data)
The app crashes with: kotlin.KotlinNullPointerException at submitData. What is the most likely cause?
Attempts:
2 left
💡 Hint
NullPointerException usually means a null value was used where not allowed.
✗ Incorrect
Passing a null data parameter to a suspend function expecting a non-null @Body causes KotlinNullPointerException. Missing suspend would cause compile error, not runtime. Response is valid. Incorrect URL causes HTTP errors, not null pointer.