Saturday, October 6, 2012

Android interview questions : Interview prepration for android development : Important question answer in Android

Updated December 2026: I rewrote this post for modern Android development. The 2012 answers are removed because many were outdated and some were wrong. The old comments are still below.

Hello friends,

I wrote the first version of these interview questions in October 2012. A lot has changed since then. Dalvik is gone, Eclipse is gone, and today almost every Android interview asks about Kotlin, coroutines and Jetpack Compose. When I read my old answers again, I also found some mistakes, so I rewrote the whole post.

The answers are short on purpose. In an interview they want to see that you understand the idea, so learn the "why", not only the definition.

Android basics

Q1. What is Android?

Android is an open-source operating system for phones, tablets, watches, TVs and cars, based on the Linux kernel. Apps are written mostly in Kotlin (or Java) and run on the Android Runtime (ART).

Q2. What is ART, and what happened to Dalvik?

ART (Android Runtime) runs the app code. It replaced Dalvik as the default runtime in Android 5.0 (Lollipop). ART compiles the app's DEX bytecode ahead of time and just in time, so apps start and run faster than on Dalvik.

Q3. What is inside an APK? And what is an AAB?

An APK is a ZIP file. It contains AndroidManifest.xml, the compiled code (classes.dex), resources (res/ and resources.arsc), assets, native libraries (lib/) and the signature.

An AAB (Android App Bundle) is the format you upload to Google Play. Play then builds smaller APKs for each device, with only the resources and code that device needs.

Q4. What are the four main app components?

  • Activity: one screen with a user interface.
  • Service: work without a UI, for example playing music.
  • BroadcastReceiver: reacts to system or app events, like "battery low".
  • ContentProvider: shares data with other apps, like the contacts app does.

All of them (except receivers registered in code) must be declared in AndroidManifest.xml.

Q5. Explain the Activity lifecycle.

onCreate() → onStart() → onResume() (the user can use the screen) → onPause() → onStop() → onDestroy(). If the user comes back to a stopped activity, onRestart() is called before onStart().

A good follow-up answer: on a configuration change, like rotating the phone, the activity is destroyed and created again. That's why we keep screen data in a ViewModel and not in the activity.

Q6. What is the difference between an explicit and an implicit Intent?

An explicit intent names the exact component to start, for example Intent(this, DetailActivity::class.java). An implicit intent only describes an action, like ACTION_VIEW with a web address, and Android finds an app that can handle it.

Q7. What is a PendingIntent?

A PendingIntent is an intent that you give to another app or to the system, so it can run it later on your app's behalf. Notifications and alarms use them. If your app targets Android 12 or higher, you must say if it is FLAG_IMMUTABLE or FLAG_MUTABLE. Use immutable unless you really need it to change.

Q8. What were sticky broadcasts, and should we use them?

A sticky broadcast stayed in the system after it was sent, so a receiver registered later still got the last value (battery status was the classic example). sendStickyBroadcast() is deprecated since Android 5.0 because it is not secure. Don't use it in new apps.

Q9. Application context or Activity context?

The Activity context is tied to the screen and is needed for UI things like dialogs and themes. The Application context lives as long as the app. Never keep an Activity context in a long-living object (a singleton, a static field), because the Activity can't be garbage collected and you get a memory leak.

Kotlin

Q10. What is the difference between val and var?

val is read-only: you can't assign it again. var can be changed. Prefer val whenever you can.

Q11. How does Kotlin handle null safety?

Types are non-null by default. A nullable type has a ?, like String?. Then you use the safe call ?., the Elvis operator ?: for a default value, or !! if you are sure it is not null (it throws an exception if it is, so avoid it).

val length = name?.length ?: 0

Q12. What is a data class? And a sealed class?

A data class holds data, and Kotlin generates equals(), hashCode(), toString() and copy() for it. A sealed class has a fixed set of subclasses, so a when expression knows all cases. It is perfect for UI states:

sealed interface UiState {
    data object Loading : UiState
    data class Success(val items: List<String>) : UiState
    data class Error(val message: String) : UiState
}

Q13. lateinit or lazy?

lateinit var is for a non-null property that you set later, for example in onCreate(). by lazy is for a val that is created the first time you use it, and then the same value is reused.

Coroutines and Flow

Q14. What is a coroutine, and why do we use it on Android?

A coroutine is a light-weight way to run code that waits (network, database) without blocking the main thread. The code still reads from top to bottom, without callbacks. It replaced AsyncTask, which is deprecated.

Q15. What are Dispatchers?

They decide which thread runs the coroutine: Dispatchers.Main for UI work, Dispatchers.IO for network and files, and Dispatchers.Default for heavy CPU work like sorting a big list.

Q16. What is the difference between launch and async?

launch starts a coroutine and doesn't return a result ("fire and forget"). async returns a Deferred, and you get the result with await(). Use async when you want to run two things in parallel and wait for both.

Q17. What are viewModelScope and lifecycleScope?

They are coroutine scopes that cancel their coroutines automatically: viewModelScope when the ViewModel is cleared, lifecycleScope when the activity or fragment is destroyed. This is called structured concurrency, and it prevents leaks and crashes from work that runs too long.

Q18. Flow, StateFlow or SharedFlow?

A Flow is a stream of values that starts when somebody collects it. A StateFlow always has a current value, so it is good for UI state. A SharedFlow is for events that many collectors can receive, like "show a message".

Architecture and Jetpack

Q19. What is a ViewModel?

A ViewModel holds the data for a screen and survives configuration changes like rotation. The UI observes its state and sends user actions to it. Here is a typical example:

class UserViewModel(private val repo: UserRepository) : ViewModel() {

    private val _state = MutableStateFlow<UiState>(UiState.Loading)
    val state: StateFlow<UiState> = _state

    fun load() {
        viewModelScope.launch {
            _state.value = try {
                UiState.Success(repo.getUsers())
            } catch (e: Exception) {
                UiState.Error("Could not load users")
            }
        }
    }
}

Q20. Explain MVVM and the repository pattern.

In MVVM, the View (activity or composable) only shows the state, the ViewModel holds the state and the logic, and the Model is the data. A repository sits between the ViewModel and the data sources (database, network), so the ViewModel doesn't care where the data comes from. Data flows down to the UI, and events flow up. This is called unidirectional data flow.

Q21. What is dependency injection, and what is Hilt?

Dependency injection means a class gets the objects it needs from outside instead of creating them itself. That makes it easy to test and to replace parts. Hilt is the Jetpack library for DI on Android, built on top of Dagger.

Q22. Why Room instead of plain SQLite?

Room is a layer on top of SQLite. It checks your SQL queries when you compile, maps rows to Kotlin objects, supports coroutines and Flow, and removes most of the boilerplate code. You write an @Entity, a @Dao and a @Database class.

Q23. SharedPreferences or DataStore?

DataStore is the modern replacement. It works with coroutines and Flow, it does its work off the main thread, and it handles errors better. SharedPreferences can block the main thread and is not recommended for new code.

Q24. How do you run background work that must finish, even if the app is closed?

Use WorkManager. It runs deferrable work reliably, even after a restart, and it supports constraints like "only on Wi-Fi" or "only while charging". For work the user is aware of right now, like music or navigation, use a foreground service. If your app targets Android 14 or higher, you must declare the foreground service type.

Jetpack Compose

Q25. What is Jetpack Compose?

Compose is the modern UI toolkit for Android. You describe the UI with Kotlin functions (composables) instead of XML layouts, and Compose updates the screen when the state changes. This is called declarative UI.

Q26. What is recomposition?

When a state value that a composable reads changes, Compose runs that composable again to update the UI. This is recomposition. Compose skips the parts whose inputs didn't change, so keep composables fast and without side effects.

Q27. remember or rememberSaveable?

remember keeps a value across recompositions. rememberSaveable also keeps it across configuration changes and process death, as long as it can be saved in a Bundle.

@Composable
fun Counter() {
    var count by rememberSaveable { mutableStateOf(0) }
    Button(onClick = { count++ }) {
        Text("Clicked $count times")
    }
}

Q28. What is state hoisting?

Moving the state up to the caller, so the composable becomes stateless: it gets the value and an onValueChange lambda as parameters. Stateless composables are easier to reuse, preview and test.

Q29. What is a side effect, and what is LaunchedEffect?

A side effect is something that changes things outside the composable, like loading data or showing a snackbar. LaunchedEffect(key) starts a coroutine when the composable enters the screen, and restarts it when the key changes.

Permissions, security and performance

Q30. How do runtime permissions work?

Since Android 6.0, dangerous permissions like camera or location must be requested while the app runs, not only in the manifest. Use ActivityResultContracts.RequestPermission(), explain to the user why you need it, and handle "denied". Since Android 13, showing notifications also needs a runtime permission (POST_NOTIFICATIONS).

Q31. What is an ANR, and how do you avoid it?

ANR means "Application Not Responding". Android shows it when the main thread is blocked too long, for example about 5 seconds without reacting to a touch. Avoid it by doing network, database and heavy work in coroutines on Dispatchers.IO or Default, never on the main thread.

Q32. What causes memory leaks, and how do you find them?

Common causes are keeping an Activity or View in a static field or singleton, listeners that are never removed, and long work that holds a reference to a screen. LeakCanary finds leaks automatically in debug builds, and the Android Studio Memory Profiler helps too.

Q33. What does R8 do?

R8 shrinks, optimizes and obfuscates the code in release builds. It removes unused code and resources and renames classes, so the app is smaller and harder to reverse-engineer. Keep rules tell R8 what it must not touch, for example classes used by reflection.

Q34. What is a nine-patch image? Do we still use it?

A nine-patch (.9.png) is a PNG with a 1-pixel border that marks which parts can stretch and where the content goes. It was popular for buttons and chat bubbles. Today we mostly use vector drawables and shapes, but nine-patch still works.

Q35. Which dialogs can you use today?

In XML-based apps: AlertDialog (or MaterialAlertDialogBuilder for the Material look), DatePickerDialog, TimePickerDialog, and DialogFragment to keep a dialog alive on rotation. ProgressDialog is deprecated, so show a progress indicator in the screen instead. In Compose, use the AlertDialog composable.

That's it for now. If you have an interview soon, don't try to learn all the answers by heart. Build a small app with a ViewModel, Room and Compose, and most of these questions will feel easy.

Hope it will help you in your interview. All the best!

Thanks!

13 comments:

  1. Replies
    1. Thanks Abhay for appreciate the quality of my blog..Yes I will soon publish more answer given link by you..

      Thanks,

      Delete
  2. Thanks for the sharing of such information we will pass it on to our readers.

    ReplyDelete
    Replies
    1. Yes sure please.. I will very thankful to you if you promote my blog..

      Delete
  3. Indian friends are resourceful!

    ReplyDelete
  4. excellent questions.....thanks for sharing us

    ReplyDelete
  5. hi, two questions about this post.
    1) i dont get the point of what is translate in android
    2) android application architecture and android architecture are they really different? i am not criticizing you but i just want to clearify my doubts

    ReplyDelete
  6. please can u tell me what the exact difference between regular bitmap and nine patch bitmap...does regular bitmap dont have resizeable features and all that in nine atch

    ReplyDelete
  7. Thank you very much sir. Really helpful :-)

    ReplyDelete
  8. Please provide me with a code for implementing gyroscope along with rotor. Rotor drawn using View and gyroscope synchronizing with the rotor.

    Thanx,
    Jeet

    ReplyDelete