Bird
Raised Fist0
Android Kotlinmobile~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of defining an API interface in Android Kotlin using Retrofit?
easy
A. To store data locally on the device
B. To specify how the app communicates with web services
C. To create UI components for the app
D. To manage app navigation between screens

Solution

  1. Step 1: Understand API interface role

    An API interface defines methods that describe how the app sends requests and receives responses from web services.
  2. Step 2: Identify the correct purpose

    It is not related to UI, local storage, or navigation but specifically to communication with web services.
  3. Final Answer:

    To specify how the app communicates with web services -> Option B
  4. Quick Check:

    API interface = communication with web services [OK]
Hint: API interfaces define web communication methods [OK]
Common Mistakes:
  • Confusing API interface with UI components
  • Thinking API interfaces handle local data storage
  • Assuming API interfaces manage app navigation
2. Which of the following is the correct way to define a GET request method in a Retrofit API interface?
easy
A. @GET("users") fun getUser(): Call<User>
B. @GET fun getUser(): User
C. fun getUser(): Call<User>
D. fun getUser() @GET("users"): Call<User>

Solution

  1. Step 1: Recall Retrofit syntax for GET

    Retrofit uses @GET annotation with the endpoint path as a string parameter before the method declaration.
  2. Step 2: Check method return type and annotation placement

    The method should return Call<User> and the annotation must be above the method signature.
  3. Final Answer:

    @GET("users") fun getUser(): Call<User> -> Option A
  4. Quick Check:

    @GET("endpoint") before method with Call return [OK]
Hint: Annotations go above method with endpoint string [OK]
Common Mistakes:
  • Placing @GET after method name
  • Omitting endpoint path in @GET
  • Returning User directly instead of Call<User>
3. Given this Retrofit interface method:
@POST("login")
fun loginUser(@Body user: User): Call<Response>
What happens when loginUser is called?
medium
A. Throws a compile-time error due to missing annotation
B. Sends a GET request to 'login' without any data
C. Sends a POST request to 'login' with user data in the body
D. Returns user data without network call

Solution

  1. Step 1: Analyze the method annotation and parameters

    The method uses @POST("login") which means it sends a POST request to the 'login' endpoint.
  2. Step 2: Understand the @Body parameter

    The @Body annotation sends the 'user' object as the request body in the POST request.
  3. Final Answer:

    Sends a POST request to 'login' with user data in the body -> Option C
  4. Quick Check:

    @POST with @Body sends data in POST request [OK]
Hint: @POST with @Body sends data in request body [OK]
Common Mistakes:
  • Confusing POST with GET request
  • Ignoring the @Body annotation
  • Assuming no network call happens
4. Identify the error in this Retrofit API interface method:
@GET("users/{id}")
fun getUser(@Path userId: Int): Call<User>
medium
A. The return type Call<User> is incorrect
B. The method is missing the @Body annotation
C. The method should use @POST instead of @GET
D. The @Path parameter name does not match the placeholder in the URL

Solution

  1. Step 1: Check the URL placeholder and @Path parameter

    The URL has {id} but the parameter is named userId, so they do not match.
  2. Step 2: Understand Retrofit @Path usage

    The name inside @Path must match the placeholder in the URL exactly to replace it correctly.
  3. Final Answer:

    The @Path parameter name does not match the placeholder in the URL -> Option D
  4. Quick Check:

    @Path name must match URL placeholder [OK]
Hint: Match @Path name exactly with URL placeholder [OK]
Common Mistakes:
  • Using different names for @Path and URL placeholder
  • Confusing @GET with @POST
  • Adding unnecessary @Body in GET request
5. You want to define an API interface method that sends a POST request to "submit" with a JSON body and expects a Map response. Which is the correct method signature?
hard
A. @POST("submit") fun submitData(@Body data: Map): Call>
B. fun submitData(@Body data: Map): Map
C. @POST fun submitData(data: Map): Call>
D. @POST("submit") fun submitData(@Body data: Map): Map

Solution

  1. Step 1: Confirm the annotation and endpoint

    The method must have @POST("submit") to specify the POST request and endpoint.
  2. Step 2: Check parameter and return types

    The parameter should be annotated with @Body to send JSON data, and the return type should be Call> for Retrofit's asynchronous call.
  3. Final Answer:

    @POST("submit") fun submitData(@Body data: Map): Call> -> Option A
  4. Quick Check:

    @POST with @Body and Call return type [OK]
Hint: Use @POST with @Body and Call<> return type [OK]
Common Mistakes:
  • Omitting endpoint path in @POST
  • Returning Map directly instead of Call<Map>
  • Missing @Body annotation on parameter
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard