Jul.11
Interview: Android – Kotlin
Queston: What are Lambda Expressions and Anonymous Functions?
Lambda expressions and anonymous functions are ‘function literals’, i.e. functions that are not declared, but passed immediately as an expression.
Lambda expression is always surrounded by curly braces, parameter declarations in the full syntactic form go inside curly braces and have optional type annotations, the body goes after an -> sign. If the inferred return type of the lambda is not Unit, the last (or possibly single) expression inside the lambda body is treated as the return value.
Anonymous function looks very much like a regular function declaration, except that its name is omitted. Its body can be either an expression or a block. We can specify return type in anonymous function.
Question: What are Coroutines?
Coroutines are a new way of writing asynchronous, non-blocking codes (and much more). Coroutines are sort of tasks that threads can execute. Threads can stop executing coroutines at some specific “suspension points”, and go do some other work. Also threads can resume executing coroutines later on, or another thread could even take over.
Question: What are CoroutineContext and CoroutineDispatcher?
Coroutines always execute in some context represented by a value of the CoroutineContext type, defined in the Kotlin standard library. CoroutineContext is a set of various elements. The main elements are the job of the coroutine and its dispatcher.
Coroutine context includes a coroutine dispatcher that determines what thread or threads the corresponding coroutine uses for its execution. CoroutineDispatcher can confine coroutine execution to a specific thread, dispatch it to a thread pool, or let it run unconfined.
Question: What are GlobalScope and CoroutineScope?
GlobalScope is used to launch top-level coroutines which are operating on application lifetime and are not cancelled prematurely.
CoroutineScope defines a scope for new coroutines. Coroutine builders are extensions on CoroutineScope and inherit CoroutineContext to automatically propagate context elements and cancellation.
Question: What are Suspend functions?
Suspend functions are at the center of every coroutine. Suspend function is a function that can be paused and resumed at a later time. These functions can execute long running operations and wait to complete without blocking threads.
Question: What are runBlocking and coroutineScope?
runBlocking runs a new coroutine and blocks the current thread interruptibly until its completion. It is not a suspend function and should not be used from a coroutine.
coroutineScope is a suspend fun. When coroutine suspends, coroutineScope function suspends as well.
Question: What are async and launch?
launch starts fire-and-forget coroutine and it returns Job.
async starts coroutine which computes some results and it returns Deferred<*>.
Question: What is Deferred?
Deferred is a kind of analog of future which encapsulates an operation that will be finished at some point in future after it’s initialization.