0
0
Android Kotlinmobile~20 mins

API interface definition in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
API Interface Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📝 Syntax
intermediate
2: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>
}
A
interface ApiService {
  @GET("users")
  suspend fun getUsers(): List&lt;User&gt;
}
B
interface ApiService {
  @GET("users")
  fun getUsers(): List&lt;User&gt;
}
C
interface ApiService {
  @GET("users")
  fun getUsers(): Call&lt;List&lt;User&gt;&gt;
}
D
interface ApiService {
  @GET("users")
  suspend fun getUsers(): List&lt;String&gt;
}
Attempts:
2 left
💡 Hint
Remember Retrofit uses suspend functions for coroutines and the return type should match the expected data.
ui_behavior
intermediate
1: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?
AThe API call runs synchronously and updates UI automatically.
BThe UI thread blocks until the API call finishes, freezing the app.
CThe API call runs on a background thread but UI updates must be done manually on the main thread.
DThe API call runs asynchronously without blocking the UI thread, allowing loading indicators to show.
Attempts:
2 left
💡 Hint
Think about how suspend functions work with coroutines and UI responsiveness.
lifecycle
advanced
2: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?
AUse lifecycleScope in the Activity to launch the coroutine for the API call.
BCall the API method directly inside the Activity without lifecycle awareness.
CUse GlobalScope to launch the coroutine for the API call.
DCall the API method inside a background thread without coroutine.
Attempts:
2 left
💡 Hint
Consider how Android lifecycleScope helps manage coroutines tied to lifecycle.
navigation
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
}
A
finish()
startActivity(Intent(this, HomeActivity::class.java))
BstartActivity(Intent(this, HomeActivity::class.java))
C
startActivity(Intent(this, HomeActivity::class.java))
finish()
DIntent(this, HomeActivity::class.java).apply { start() }
Attempts:
2 left
💡 Hint
Think about removing LoginActivity from back stack after navigation.
🔧 Debug
expert
2: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?
AThe Response<Unit> return type is invalid and causes null pointer.
BThe data parameter passed to submitData is null.
CThe Retrofit interface method is missing suspend keyword.
DThe @POST annotation URL is incorrect causing null pointer.
Attempts:
2 left
💡 Hint
NullPointerException usually means a null value was used where not allowed.