1. What is CoroutineScope?
AdvancedAnswer: Defines scope for coroutine lifecycle
CoroutineScope defines coroutine lifecycle. Provides context and manages child coroutines. Use GlobalScope, lifecycleScope, viewModelScope. Structured concurrency.
24 questions that come up in Kotlin technical interviews, each with the answer and an explanation of why it is right.
Test yourself — 90 question bankAnswer: Defines scope for coroutine lifecycle
CoroutineScope defines coroutine lifecycle. Provides context and manages child coroutines. Use GlobalScope, lifecycleScope, viewModelScope. Structured concurrency.
Answer: Functions taking functions as parameters or returning functions
Higher-order functions take functions as parameters or return functions. Example: fun operate(x: Int, op: (Int) -> Int) = op(x). Foundation of functional programming.
Answer: Modern statically-typed programming language for JVM, Android, JS
Kotlin is a modern, statically-typed programming language developed by JetBrains. It runs on JVM, compiles to JavaScript, and is Google's preferred language for Android development.
Answer: Set of elements defining coroutine behavior
CoroutineContext is indexed set of elements: Job, Dispatcher, CoroutineName, ExceptionHandler. Combine with +. Child inherits parent context.
Answer: Concise syntax, null safety, modern features
Kotlin offers concise syntax (less boilerplate), null safety, extension functions, coroutines, smart casts, and many modern features while maintaining 100% Java interoperability.
Answer: Configures object and returns it
apply executes block on object and returns object: person.apply { name = "John"; age = 30 }. Use for object initialization. this refers to object.
Answer: Executes lambda and returns result
let executes lambda with object as it and returns lambda result: name?.let { println(it.length) }. Good for null checks. it refers to object.
Answer: val (immutable) or var (mutable)
Use val for immutable (read-only) variables: val x = 5. Use var for mutable variables: var y = 10. val is preferred for immutability.
Answer: Determines thread for coroutine execution
Dispatcher determines thread: Dispatchers.Main (UI), IO (blocking IO), Default (CPU-intensive), Unconfined (inherits). Set with withContext or CoroutineScope.
Answer: Switches coroutine context temporarily
withContext switches context for block: withContext(Dispatchers.IO) { readFile() }. Suspends, switches, executes, returns to original. Returns last expression.
Answer: val is immutable, var is mutable
val (value) is read-only, assigned once. var (variable) is mutable, can be reassigned. val x = 5 cannot be changed. var y = 5 can be changed to y = 10.
Answer: Performs action and returns object
also performs action and returns original object: list.also { println("Size: ${it.size}") }. Use for side effects. it refers to object.
Answer: Parent waits for children, propagates cancellation
Structured concurrency ensures parent waits for children. Canceling parent cancels children. Exceptions propagate. Prevents coroutine leaks. Foundation of Kotlin coroutines.
Answer: fun functionName(params): ReturnType
Use fun keyword: fun add(a: Int, b: Int): Int { return a + b }. Single-expression functions: fun add(a: Int, b: Int) = a + b.
Answer: Executes block and returns result
run executes block and returns result: val result = person.run { "$name is $age" }. Use for computing value. this refers to object.
Answer: Cancellable thing with lifecycle
Job represents coroutine lifecycle. Returned by launch. States: New, Active, Completing, Completed, Cancelling, Cancelled. Cancel: job.cancel(). Join: job.join().
Answer: Compiler automatically determines type
Type inference allows compiler to deduce types: val x = 5 (Int inferred). Makes code more concise while maintaining type safety.
Answer: Calls block with receiver as this
with is non-extension function taking receiver: with(person) { println(name) }. Returns lambda result. Use for multiple operations on object. this refers to receiver.
Answer: Restricted class hierarchy with known subclasses
Sealed class restricts inheritance: sealed class Result. Subclasses must be in same file. Great with when for exhaustive handling. Represents restricted alternatives.
Answer: Job where child failure doesn't affect siblings
SupervisorJob: child failure doesn't cancel parent or siblings. Regular Job: any child failure cancels all. Use for independent operations that shouldn't affect each other.
Answer: Type system distinguishes nullable and non-nullable types
Null safety prevents NullPointerException. Types are non-nullable by default. Use ? for nullable: String? can be null, String cannot. Compile-time null safety.
Answer: Add ? after type: String?
Append ? to type for nullable: var name: String? = null. Without ?, variable cannot be null: var name: String = "John" (cannot assign null).
Answer: Asynchronous stream of values
Flow is cold asynchronous stream: flow { emit(1); emit(2) }. Suspending, cancellable. Terminal operators: collect, first, reduce. Operators: map, filter, transform.
Answer: Unpacks object into multiple variables
Destructuring unpacks objects: val (name, age) = person. Works with data classes, Pair, List. Uses componentN() functions. Max 5 components without custom implementation.
The full Kotlin bank has 90 questions across 3 difficulty levels — timed, shuffled, and scored.
Take the Kotlin quiz