Interview Questions & Answers
Android is an open-source, Linux-based operating system designed primarily for mobile devices such as smartphones and tablets.
A new instance of the activity is created and pushed onto the back stack every time it's launched.
If an instance of the activity is already at the top of the back stack, a new instance isn't created.
Instead, the system reuses the existing instance and calls its onNewIntent() method with the new intent
This launch mode ensures that there is only one instance of the activity in a task. If an instance of the activity already exists anywhere in the back stack, the system doesn't create a new instance
This is the most restrictive launch mode. It's similar to singleTask, but the activity is launched in its own, new task and is the only activity allowed in that task
: Represents a single screen with a user interface. It's what the user interacts with. An app can have multiple activities, and each has its own lifecycle
A component that runs in the background to perform long-running operations, like playing music or fetching data from a network, without a user interface
A component that responds to system-wide broadcast announcements (or Intents). An example is a receiver that listens for a "low battery" broadcast to save data
Manages access to a structured set of data. It's a standardized way to share data between applications. For instance, the Contacts app uses a content provider to share contact information with other apps
Intent is a messaging object used to request an action from another app component.
Specifies the component to be started by name.
val intent = Intent(this, ActivityTwo::class.java) intent.putExtra("Value1", "This is ActivityTwo") intent.putExtra("Value2", "This Value two for ActivityTwo") startActivity(intent)
Specifies the action to be performed and allows the Android system to find the best component to handle it
val i = Intent(Intent.ACTION_VIEW, Uri.parse("http://www.interviewbit.com")) startActivity(i)
In Android, a Service is an application component that can perform long-running operations in the background, without a user interface. Services are primarily used for tasks that shouldn't be interrupted by the user navigating away from an activity, such as playing music, fetching data from a network, or monitoring a user's location
There are three main types of services in Android, each with a different purpose and set of behaviors
A foreground service performs a task that is noticeable to the user. It must display a persistent notification in the status bar while it's running
A background service performs tasks that are not directly noticeable to the user. These services run without a notification. With modern Android versions (Android 8.0 and higher), background services have strict limitations to conserve battery and system resources. They are generally only used for short, non-critical tasks. For longer background tasks, Android now recommends using WorkManager or JobScheduler
A bound service provides a client-server interface that allows other components (like activities) to bind to it and interact with it. A bound service runs only as long as another application component is bound to it. Once all clients unbind, the service is destroyed
Note: For longer background tasks, Android recommends using WorkManager or JobScheduler.
Model-View-ViewModel - Separates UI from business logic
Application's data and business logic. Independent of UI.
UI layer (Activity/Fragment). Displays data and forwards user interactions.
Intermediary between Model and View. Holds presentation logic and exposes data through observables.
View β ViewModel β Model β ViewModel β View (automatic UI updates)
Issue: Can lead to "God Activity" problem
Advantage: Better testability
LiveData is an observable data holder class that is lifecycle-aware.
A class designed to store and manage UI-related data in a lifecycle-conscious way.
Key Point: ViewModel instances are not destroyed during configuration changes, avoiding data loss and unnecessary network/database calls.
Activities, Fragments, Views, ViewModels - Everything user sees
Pure Kotlin/Java - No Android dependencies
Repositories, APIs, Databases - Data handling
Modern Programming Language for JVM
Feature | val | var |
---|---|---|
Mutability | Read-only (immutable) | Mutable |
Assignment | Cannot be reassigned | Can be reassigned |
Java Equivalent | final keyword | Regular variable |
val name = "Kotlin" // Cannot be changed var age = 25 // Can be changed // name = "Java" // Compilation error age = 26 // Valid
Feature that helps eliminate NullPointerException at compile time.
?
after type to allow null?.
): Access method/property on nullable object?:
): Provide default value for nullable expression!!
): Convert nullable to non-null (use with caution)var name: String = "Kotlin" // Non-nullable var nullableName: String? = null // Nullable val length = nullableName?.length ?: 0 // Safe call + Elvis
Special class designed to hold data. Compiler automatically generates useful methods.
data class User(val name: String, val age: Int) val user1 = User("Alice", 25) val user2 = user1.copy(age = 26) println(user1) // User(name=Alice, age=25)
Both run on the Java Virtual Machine (JVM) and can use existing Java libraries and frameworks.
Feature | List | Array |
---|---|---|
Size | Dynamic (can grow/shrink) | Fixed size |
Type Flexibility | Can store different types with generics | Homogeneous (single type) |
Modification | Convenient add/remove methods | Fixed size, overwrite only |
Performance | Some overhead for dynamic operations | Better performance, contiguous memory |
Used with mutable properties that will be assigned later.
var
properties onlyclass MyActivity : Activity() { private lateinit var binding: ActivityMainBinding override fun onCreate(savedInstanceState: Bundle?) { binding = DataBindingUtil.setContentView(...) } }
For expensive object creation that should be delayed until first access.
val
properties onlyval expensiveObject by lazy { // This block runs only once, on first access createExpensiveObject() }
Declared directly in the class header. Concise and can initialize properties directly.
class User(val name: String, val age: Int) // Usage val user = User("Alice", 25)
Defined inside the class using the constructor keyword. Useful for additional initialization logic.
class User { var name: String var age: Int constructor(name: String, age: Int) { this.name = name this.age = age } }
Modifier | Accessibility | Scope |
---|---|---|
public (default) | Accessible everywhere | Global |
private | Same class or file only | Local |
protected | Class and its subclasses | Inheritance hierarchy |
internal | Same module only | Module-wide |
class MyClass { public val publicProperty = "Everyone can see" private val privateProperty = "Only MyClass can see" protected val protectedProperty = "MyClass and subclasses" internal val internalProperty = "Same module only" }
A concurrency design pattern that simplifies asynchronous programming.
Lives for entire application lifetime. Use with caution - can cause memory leaks.
Structured way to manage coroutines. Automatically cancels when scope is canceled.
class MyViewModel : ViewModel() { fun fetchData() { viewModelScope.launch { // This coroutine will be cancelled when ViewModel is cleared val data = repository.getData() _liveData.value = data } } }
Dispatcher | Purpose | Use Case |
---|---|---|
Dispatchers.Main | Main/UI thread | UI updates, view interactions |
Dispatchers.IO | I/O operations | Network calls, file operations, database |
Dispatchers.Default | CPU-intensive tasks | Sorting, calculations, data processing |
Dispatchers.Unconfined | Starts in caller thread | Not recommended for most cases |
viewModelScope.launch { val data = withContext(Dispatchers.IO) { // Network call on IO thread repository.fetchData() } // Back to Main thread for UI update updateUI(data) }
Feature | Coroutines | Threads |
---|---|---|
Weight | Lightweight | Heavy (OS-managed) |
Blocking | Non-blocking | Blocking |
Resource Usage | Low memory overhead | High memory overhead |
Scalability | Can create thousands | Limited by system resources |
Management | Kotlin runtime | Operating system |
Special functions that can be paused and resumed later without blocking the thread.
suspend fun fetchData(): String { delay(2000) // Simulates network call return "Data fetched successfully" } fun main() = runBlocking { println("Start") val data = fetchData() // Suspend function call println(data) println("End") }
Special functions that create a temporary scope for an object to make code more concise and readable.
Function | Returns | Object Reference | Use Case |
---|---|---|---|
let | Lambda result | it | Null-checks, chaining |
run | Lambda result | this | Object initialization |
with | Lambda result | this | Multiple operations on object |
apply | Object itself | this | Object configuration |
also | Object itself | it | Additional actions (logging) |
// let - null checks val name: String? = "Kotlin" name?.let { println("Name is $it") } // apply - object configuration val person = Person().apply { name = "John" age = 30 } // run - initialization with result val result = "Kotlin".run { println("This is $this") length // returns 6 } // with - multiple operations val builder = StringBuilder() with(builder) { append("Hello, ") append("World!") } // also - additional actions val numbers = mutableListOf(1, 2, 3) .also { println("List: $it") } .add(4)
Ready for your Android & Kotlin Interview