0
0
Android Kotlinmobile~20 mins

OkHttp interceptors in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
OkHttp Interceptor Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What happens when an OkHttp interceptor modifies the request URL?
Consider an OkHttp interceptor that changes the request URL before proceeding. What will the server receive?
Android Kotlin
val interceptor = Interceptor { chain ->
  val newRequest = chain.request().newBuilder()
    .url("https://api.example.com/modified")
    .build()
  chain.proceed(newRequest)
}
AThe server receives the original URL from the request before modification.
BThe request fails because the URL cannot be changed in an interceptor.
CThe server receives the modified URL set by the interceptor.
DThe interceptor causes an infinite loop and the request never completes.
Attempts:
2 left
💡 Hint
Think about what the interceptor does before calling proceed.
lifecycle
intermediate
2:00remaining
When is an OkHttp interceptor called during a request?
At what point in the OkHttp request lifecycle does an interceptor run?
AOnly after the response is received, never before the request.
BAfter the response is received but before it is returned to the caller.
COnly before the request is sent, never after the response.
DBefore the request is sent and after the response is received, wrapping the entire call.
Attempts:
2 left
💡 Hint
Interceptors can modify both request and response.
🔧 Debug
advanced
2:00remaining
Why does this interceptor cause a stack overflow error?
Analyze the following interceptor code. Why does it cause a stack overflow?
Android Kotlin
val interceptor = Interceptor { chain ->
  val request = chain.request()
  chain.proceed(request)
  chain.proceed(request)
}
ACalling chain.proceed(request) twice causes infinite recursion leading to stack overflow.
BThe interceptor modifies the request incorrectly causing a stack overflow.
CThe interceptor does not call proceed, so the request never completes causing stack overflow.
DThe interceptor uses a wrong request object causing stack overflow.
Attempts:
2 left
💡 Hint
Check how many times proceed is called.
🧠 Conceptual
advanced
2:00remaining
What is the difference between application and network interceptors in OkHttp?
Choose the correct distinction between application interceptors and network interceptors.
AApplication interceptors observe the full request and response, including retries; network interceptors see data just before network transmission.
BNetwork interceptors run before application interceptors and can modify the UI directly.
CApplication interceptors only modify responses; network interceptors only modify requests.
DThere is no difference; both interceptors behave identically.
Attempts:
2 left
💡 Hint
Think about when retries and caching happen.
📝 Syntax
expert
2:00remaining
What error does this interceptor code produce?
Examine the Kotlin interceptor code below. What error will it cause?
Android Kotlin
val interceptor = Interceptor { chain ->
  val request = chain.request()
  val response = chain.proceed(request)
  return response
  println("Intercepted")
}
ACompilation error due to missing semicolon after return statement.
BSyntaxError: 'return' cannot be used inside lambda expressions.
CNo error; code runs and prints "Intercepted" after returning response.
DRuntimeException because println is unreachable code after return.
Attempts:
2 left
💡 Hint
Consider Kotlin lambda expression rules for return statements.