🔥 Android Interview Q&A — AIO App Inc (Kotlin + Coroutines)

1. What is a Data Class?

A data class in Kotlin is used to hold data. It automatically provides equals(), hashCode(), toString(), copy(), and componentN() functions.

data class User(val name: String, val age: Int)

2. What is Enum? Can I use multiple instances?

Enum represents a fixed set of constants. Each enum constant is a singleton, meaning you cannot create multiple instances of it.

enum class Direction { NORTH, SOUTH, EAST, WEST }

3. Difference between View and ViewGroup

4. lateinit vs lazy

Featurelateinitlazy
TypeMutable (var)Immutable (val)
Init timeLater (manually)On first access
Use caseViews, DI fieldsExpensive objects

5. Coroutine Exception Handling (Without try-catch)

If a NumberFormatException occurs, the coroutine’s parent scope cancels — can crash the app. To avoid it, use CoroutineExceptionHandler:


val handler = CoroutineExceptionHandler { _, e -> println("Caught: $e") }
GlobalScope.launch(handler) {
    repeat(1000) {
        println(it)
        if (it == 500) throw NumberFormatException()
    }
}
    

6. async vs launch

FunctionReturnsPurpose
launchJobFire and forget
asyncDeferred<T>Returns result via await()

7. Cancel Coroutine after 3 Seconds


val job = CoroutineScope(Dispatchers.Default).launch {
    repeat(1000) {
        println(it)
        delay(1000)
    }
}
delay(3000)
job.cancel()
    

8. Extension Function Example


fun String.capitalizeName(): String = replaceFirstChar { it.uppercase() }
println("talha".capitalizeName()) // Output: Talha
    

Extension functions let you add functionality to existing classes without inheritance or modifying their source code.

9. Hot Flow vs Cold Flow

TypeBehaviorExample
Cold FlowStarts emitting when collectedflow { emit(1) }
Hot FlowEmits regardless of collectorsStateFlow, SharedFlow

10. Why StateFlow has Initial Value, but SharedFlow doesn't?

StateFlow always holds a current state, so it needs an initial value. SharedFlow emits events, not state — no initial value required.

11. Collect Multiple Flows with One Collector


combine(flow1, flow2, flow3, flow4) { f1, f2, f3, f4 ->
    listOf(f1, f2, f3, f4)
}.collect { println(it) }
    

12. Fastest Way to Store Large API Arrays in Room

Use bulk insert with @Insert(onConflict = REPLACE) inside a transaction.


@Dao
interface CategoryDao {
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertAll(categories: List)
}

db.withTransaction {
    dao.insertAll(bigList)
}
    

13. Print Only Category Names (Without Loop)


val categories = arrayListOf(
  Category(1, "category1"),
  Category(2, "category2"),
  Category(3, "category3")
)

println(categories.joinToString(", ") { it.name })
// Output: category1, category2, category3