0
0
Android Kotlinmobile~20 mins

Retrofit setup in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Retrofit Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📝 Syntax
intermediate
2:00remaining
Correct Retrofit Builder Syntax
Which option correctly creates a Retrofit instance with base URL "https://api.example.com/" and Gson converter?
Aal retrofit = Retrofit.Builder().baseUrl("https://api.example.com/").addConverterFactory(GsonConverterFactory.create()).build()
Bval retrofit = Retrofit.Builder().baseUrl("https://api.example.com").addConverterFactory(GsonConverterFactory).build()
C)(dliub.))(etaerc.yrotcaFretrevnoCnosG(yrotcaFretrevnoCdda.)"/moc.elpmaxe.ipa//:sptth"(lrUesab.)(redliuB.tiforteR = tiforter lav
Dval retrofit = Retrofit.Builder().baseUrl("https://api.example.com/").addConverterFactory(GsonConverterFactory.create()).build()
Attempts:
2 left
💡 Hint
Remember to call the create() method with parentheses and include the trailing slash in the base URL.
ui_behavior
intermediate
2:00remaining
Retrofit Network Call Behavior
What happens when you call a Retrofit service method that returns Call and you execute it asynchronously with enqueue()?
AThe network call runs on a background thread and the callback methods onResponse or onFailure are called on the main thread.
BThe network call runs on the main thread and blocks the UI until it finishes.
CThe network call runs on a background thread but the callback methods are called on a background thread.
DThe network call runs synchronously and returns the response immediately.
Attempts:
2 left
💡 Hint
Think about how Retrofit handles threading for asynchronous calls.
lifecycle
advanced
2:00remaining
Handling Retrofit Call Cancellation
Which option correctly cancels an ongoing Retrofit Call to avoid memory leaks when an Android Activity is destroyed?
Android Kotlin
var call: Call<ResponseBody>? = null

fun fetchData() {
  call = retrofitService.getData()
  call?.enqueue(object : Callback<ResponseBody> {
    override fun onResponse(call: Call<ResponseBody>, response: Response<ResponseBody>) {
      // handle response
    }
    override fun onFailure(call: Call<ResponseBody>, t: Throwable) {
      // handle failure
    }
  })
}

fun onDestroy() {
  // What to do here?
}
Acall = null
Bcall?.cancel()
Ccall?.clone()
Dcall?.enqueue(null)
Attempts:
2 left
💡 Hint
To stop a network call, you must explicitly cancel it.
navigation
advanced
2:00remaining
Retrofit and Navigation Timing
You want to navigate to a new screen only after a successful Retrofit network response. Which approach ensures navigation happens at the right time?
ACall navigation code before enqueue()
BCall navigation code immediately after enqueue() without waiting
CCall navigation code inside onResponse callback after checking response.isSuccessful
DCall navigation code inside onFailure callback
Attempts:
2 left
💡 Hint
When does Retrofit notify you of a successful response?
🧠 Conceptual
expert
2:00remaining
Retrofit Converter Factory Role
What is the main role of a Converter Factory like GsonConverterFactory in Retrofit setup?
ATo convert JSON response data into Kotlin/Java objects and vice versa for requests
BTo manage network connection retries automatically
CTo handle UI thread synchronization for network calls
DTo cache network responses locally on the device
Attempts:
2 left
💡 Hint
Think about how Retrofit transforms raw data into usable objects.