Retrofit is a type-safe HTTP client for Android and Java. It helps you easily connect your app to web services by turning HTTP API into Kotlin interfaces.
Click to reveal answer
beginner
Which dependency do you add to your build.gradle to use Retrofit?
You add implementation 'com.squareup.retrofit2:retrofit:2.9.0' to your app's build.gradle file to include Retrofit.
Click to reveal answer
intermediate
How do you create a Retrofit instance in Kotlin?
Use Retrofit.Builder() to set the base URL and converter, then call build(). For example: val retrofit = Retrofit.Builder()
.baseUrl("https://api.example.com/")
.addConverterFactory(GsonConverterFactory.create())
.build()
Click to reveal answer
intermediate
What is the role of a ConverterFactory in Retrofit?
ConverterFactory converts JSON or other data formats from the web service into Kotlin objects and vice versa. GsonConverterFactory is commonly used for JSON.
Click to reveal answer
intermediate
How do you define an API interface for Retrofit?
Create a Kotlin interface with functions annotated with HTTP methods like @GET or @POST. Each function returns a Call or suspend function for coroutines. Example: interface ApiService {
@GET("users")
suspend fun getUsers(): List<User>
}
Click to reveal answer
What is the first step to use Retrofit in your Android app?
AAdd Retrofit dependency to build.gradle
BCreate an API interface
CCall the API directly
DWrite XML layout
✗ Incorrect
You must first add the Retrofit library dependency to your project before using it.
Which Retrofit component sets the base URL for API calls?
ARetrofit.Builder().baseUrl()
BConverterFactory
CAPI interface
DCall object
✗ Incorrect
The baseUrl() method in Retrofit.Builder sets the root URL for all API requests.
What does GsonConverterFactory do in Retrofit?
AManages API keys
BHandles network errors
CConverts JSON to Kotlin objects
DCreates UI layouts
✗ Incorrect
GsonConverterFactory converts JSON responses into Kotlin data classes and vice versa.
How do you declare a GET request in a Retrofit API interface?
AIn AndroidManifest.xml
BUsing a JSON file
CInside build.gradle
D@GET annotation on a function
✗ Incorrect
You use the @GET annotation on a function inside the API interface to declare a GET request.
Which return type is commonly used for Retrofit API calls with Kotlin coroutines?
AVoid
Bsuspend function returning data
CString
DInt
✗ Incorrect
Using suspend functions allows Retrofit to work smoothly with Kotlin coroutines for asynchronous calls.
Explain the steps to set up Retrofit in an Android Kotlin project.
Think about what you need to add, build, and define before making API calls.
You got /4 concepts.
Describe how Retrofit converts JSON responses into Kotlin objects.
Focus on the part that changes raw data into usable app data.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of Retrofit in Android Kotlin development?
easy
A. To manage local databases
B. To simplify network calls and convert web data into Kotlin objects
C. To create user interfaces easily
D. To handle app navigation between screens
Solution
Step 1: Understand Retrofit's role
Retrofit is a library designed to make network calls easier and convert JSON or XML responses into Kotlin objects automatically.
Step 2: Compare with other options
Options A, B, and D relate to UI, databases, and navigation, which are not Retrofit's purpose.
Final Answer:
To simplify network calls and convert web data into Kotlin objects -> Option B
Quick Check:
Retrofit = Network calls + data conversion [OK]
Hint: Retrofit = network calls + data conversion [OK]
Common Mistakes:
Confusing Retrofit with UI or database libraries
Thinking Retrofit manages navigation
Assuming Retrofit is for local storage
2. Which of the following is the correct way to start building a Retrofit instance in Kotlin?
easy
A. val retrofit = Retrofit.newInstance("https://api.example.com")
B. val retrofit = Retrofit.create().baseUrl("https://api.example.com")
C. val retrofit = Retrofit.Builder().baseUrl("https://api.example.com").build()
D. val retrofit = Retrofit.Builder().create("https://api.example.com")
Solution
Step 1: Recall Retrofit Builder syntax
The correct way is to use Retrofit.Builder(), set the baseUrl, and then call build() to create the instance.
Step 2: Check each option
val retrofit = Retrofit.Builder().baseUrl("https://api.example.com").build() uses the correct Builder pattern. Options A, B, and C use incorrect methods or order.
Final Answer:
val retrofit = Retrofit.Builder().baseUrl("https://api.example.com").build() -> Option C
Quick Check:
Builder + baseUrl + build() = correct setup [OK]
Hint: Use Builder, set baseUrl, then build() [OK]
Common Mistakes:
Calling create() before build()
Using newInstance() which doesn't exist
Passing URL to create() instead of baseUrl()
3. Given this Retrofit setup code snippet, what will be the output if you call service.getUsers() and the server returns JSON data?
val retrofit = Retrofit.Builder()
.baseUrl("https://api.example.com/")
.addConverterFactory(GsonConverterFactory.create())
.build()
interface ApiService {
@GET("users")
suspend fun getUsers(): List<User>
}
val service = retrofit.create(ApiService::class.java)
medium
A. A raw JSON string from the server
B. A null value because the call is not executed
C. An error because GsonConverterFactory is missing
D. A List of User objects parsed from the JSON response
Solution
Step 1: Understand the converter role
GsonConverterFactory converts JSON responses into Kotlin objects automatically.
Step 2: Analyze the service call
Calling getUsers() returns a List<User> parsed from JSON because of the converter and suspend function.
Final Answer:
A List of User objects parsed from the JSON response -> Option D
Quick Check:
GsonConverterFactory + suspend fun = parsed objects [OK]
Hint: GsonConverterFactory parses JSON to objects [OK]
Common Mistakes:
Expecting raw JSON instead of parsed objects
Forgetting to add converter factory
Not understanding suspend functions
4. Identify the error in this Retrofit setup code:
val retrofit = Retrofit.Builder()
.baseUrl("https://api.example.com")
.addConverterFactory(GsonConverterFactory)
.build()
medium
A. Missing parentheses after GsonConverterFactory
B. baseUrl should not have a trailing slash
C. Retrofit.Builder() should be Retrofit.newBuilder()
D. build() should be called before addConverterFactory
Solution
Step 1: Check addConverterFactory usage
GsonConverterFactory is a class; it requires calling create() method or parentheses to instantiate.
Step 2: Verify other parts
baseUrl can have or not have trailing slash; Retrofit.Builder() is correct; build() must be last.
Final Answer:
Missing parentheses after GsonConverterFactory -> Option A
Quick Check:
addConverterFactory needs instance, not class [OK]
Hint: Call create() on converter factory class [OK]
Common Mistakes:
Passing class instead of instance to addConverterFactory
Confusing baseUrl format
Wrong order of builder methods
5. You want to create a Retrofit instance that supports both JSON and XML responses. Which setup correctly adds both converters?
hard
A. Retrofit.Builder().baseUrl("https://api.example.com/").addConverterFactory(SimpleXmlConverterFactory.create()).addConverterFactory(GsonConverterFactory.create()).build()
B. Retrofit.Builder().baseUrl("https://api.example.com/").addConverterFactory(GsonConverterFactory.create()).addConverterFactory(SimpleXmlConverterFactory).build()
C. Retrofit.Builder().baseUrl("https://api.example.com/").addConverterFactory(GsonConverterFactory).addConverterFactory(SimpleXmlConverterFactory).build()
D. Retrofit.Builder().baseUrl("https://api.example.com/").addConverterFactory(GsonConverterFactory.create()).build().addConverterFactory(SimpleXmlConverterFactory.create())
Solution
Step 1: Understand converter order importance
Retrofit tries converters in order added. XML converter should come first to handle XML, then JSON.
Step 2: Check method chaining and instantiation
Converters must be instantiated with create(). Retrofit.Builder().baseUrl("https://api.example.com/").addConverterFactory(SimpleXmlConverterFactory.create()).addConverterFactory(GsonConverterFactory.create()).build() adds XML first, then JSON, correctly.
Step 3: Eliminate wrong options
Retrofit.Builder().baseUrl("https://api.example.com/").addConverterFactory(GsonConverterFactory.create()).addConverterFactory(SimpleXmlConverterFactory.create()).build() reverses order; C misses create() calls; D calls addConverterFactory after build(), which is invalid.
Final Answer:
Retrofit.Builder().baseUrl("https://api.example.com/").addConverterFactory(SimpleXmlConverterFactory.create()).addConverterFactory(GsonConverterFactory.create()).build() -> Option A
Quick Check:
Converters order matters; XML first, then JSON [OK]
Hint: Add XML converter before JSON converter [OK]
Common Mistakes:
Wrong order of converters causing parsing errors
Not calling create() on converters
Adding converters after build() call
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.