Friday, May 30, 2025

Android Kotlin Interview Questions for 5 yrs Experience level part 1

  Here are Interview Questions for the 3-5+yrs of Experience level. Get ready for in-depth discussion during a face-to-face interview: 

You can Listen these Questions Answers by using button below. Make sure you have TTS enabled and English Languages Selected.


Web Speech Synthesis Demo


1. What is the Android Architecture?

The Android architecture is a layered system that enables Android OS to function efficiently on mobile devices. It is designed to provide an abstraction between the device hardware and the user applications.

  • Linux Kernel: The foundation of Android, it handles core system services like security, memory management, process management, network stack, and hardware drivers (camera, Wi-Fi, audio, etc.). Although Android uses Linux kernel, it’s customized specifically for mobile devices.
  • Hardware Abstraction Layer (HAL): HAL acts as a bridge between hardware drivers and higher-level APIs. It provides standard interfaces so that hardware vendors can implement their drivers without modifying Android’s core framework.
  • Android Runtime (ART): ART executes Android applications. Unlike the previous Dalvik VM which used Just-In-Time (JIT) compilation, ART uses Ahead-Of-Time (AOT) compilation to compile apps during installation, improving performance and battery life. ART also provides garbage collection, threading, and other runtime services.
  • Native Libraries: A set of C/C++ libraries providing functionalities like SQLite (database), WebKit (browser engine), OpenGL (graphics), libc, and media codecs. These are used by Android components and apps.
  • Application Framework: A higher-level API layer exposing the core system services to apps, such as Activity Manager (manages app lifecycle), Window Manager (manages screen layout), Content Providers (data sharing), Notification Manager, etc.
  • Applications: The top layer includes pre-installed system apps (Phone, Contacts, Browser) and user-installed apps. Apps interact with the framework APIs and underlying layers to provide the user experience.

2. Difference between Service, IntentService, and JobIntentService

  • Service:
    A Service is an Android component that runs in the background to perform long-running operations without interacting with the user. It runs on the main (UI) thread by default, so heavy work should be offloaded to worker threads. A Service needs to be explicitly stopped or stopped by the system.
  • IntentService:
    An IntentService is a subclass of Service that handles asynchronous requests (expressed as Intents) on a dedicated worker thread. It automatically stops itself when the work is done. It’s easy to use for short background tasks but is deprecated in Android 11 (API 30) because of background execution limits introduced in newer Android versions.
  • JobIntentService:
    Designed to replace IntentService for background work, especially for Android Oreo (API 26) and above where strict background execution limits are enforced. It uses JobScheduler internally to schedule tasks that are executed even if the app is in the background. It’s backward compatible for earlier Android versions.

3. What is Context in Android?

Context is an abstract class that provides access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting intents, etc. It is central to many Android APIs.

  • Application Context:
    Lives as long as the app lives, and is not tied to any specific activity or UI. It is suitable for operations like accessing resources or singletons that should not depend on UI lifecycle.
  • Activity Context:
    Associated with a particular Activity and can be destroyed if the activity is destroyed. It should be used for UI-related operations such as inflating views, showing dialogs, or launching new activities.
  • Service Context:
    Provided within a Service, useful for operations that don’t require UI.

Important Note: Using the wrong Context (e.g., holding an Activity Context beyond its lifecycle) can cause memory leaks, so it’s crucial to choose the appropriate Context.


4. What is the Android Manifest used for?

The AndroidManifest.xml file is a mandatory file in every Android app project. It tells the Android system essential information about your app:

  • Declaring Components: Every Activity, Service, Broadcast Receiver, and Content Provider must be declared here for the system to recognize and manage them.
  • Permissions: Declares what permissions your app needs (e.g., INTERNET, CAMERA) so the system can enforce security.
  • Hardware and Software Features: Specifies required device features (GPS, camera) so Play Store can filter compatible devices.
  • App Metadata: Defines application attributes like theme, icon, app label, supported screen sizes, and launcher activity.
  • Intent Filters: Allows your app to respond to system or custom events, for example, to receive incoming calls or handle deep links from URLs.

The manifest is critical for app lifecycle management and integration with the Android OS.


5. What’s the difference between Activity and Fragment?

  • Activity:
    An Activity represents a single screen in an Android app. It hosts UI components and responds to user interaction. Each activity runs independently and manages its own lifecycle from creation to destruction.
  • Fragment:
    A Fragment is a modular section of an activity. It represents a behavior or a portion of the UI that can be combined with other fragments to create a multi-pane UI. Fragments depend on their hosting activity for lifecycle management and allow dynamic and reusable UI components.

Key differences:

  • Activities are independent, while Fragments must be hosted inside activities.
  • Fragments enable more flexible UI designs, especially on large screens.
  • Fragments allow UI reuse and dynamic UI changes at runtime without changing activities.
  • Managing multiple fragments is easier with the FragmentManager, which handles back stack and transactions.

6. Explain ViewModel in MVVM

A ViewModel is a lifecycle-aware component designed to store and manage UI-related data in a way that survives configuration changes like screen rotations.

  • It acts as a communication center between the Repository (data source) and the UI (Activity/Fragment).
  • ViewModel holds the data that the UI needs and prepares it for display, avoiding the need to reload data after configuration changes.
  • It helps to separate UI logic from business/data logic, improving modularity and testability.
  • ViewModels should never hold references to Activities or Views directly to avoid memory leaks.

7. What is LiveData?

LiveData is an observable data holder class that is lifecycle-aware, meaning:

  • It respects the lifecycle of Activities, Fragments, or Services.
  • Observers (usually UI components) receive updates only when they are in an active lifecycle state (e.g., started or resumed).
  • This prevents crashes caused by UI updates when the activity/fragment is not active.
  • LiveData automatically manages subscription/unsubscription as lifecycle changes, reducing boilerplate code.
  • It supports transformations like map() and switchMap() to manipulate data streams.

8. How do you handle configuration changes?

Configuration changes (like screen rotations, keyboard availability) cause Android to destroy and recreate the running activity by default.

Handling strategies:

  • ViewModel: Retains UI data and survives configuration changes, so you don’t need to reload data.
  • onSaveInstanceState(Bundle): Save UI state (like scroll position, text input) before the activity is destroyed, and restore it in onCreate() or onRestoreInstanceState().
  • Retain Fragments: Mark fragments with setRetainInstance(true) to keep them across config changes (deprecated with Jetpack recommended approaches).
  • Handle manually: Prevent recreation via android:configChanges in Manifest (not recommended generally as it can cause maintenance challenges).

9. Difference between onCreate() and onStart()

Both are lifecycle callback methods of an Activity, but they serve different purposes:

  • onCreate():
    Called once when the activity is first created. Used to initialize UI components, bind data, and set up resources (e.g., setContentView()). Heavy initialization code typically goes here.
  • onStart():
    Called every time the activity becomes visible to the user. This happens after onCreate() or when returning from the background. Suitable for starting animations, refreshing UI, or acquiring resources needed while visible.

10. What is a Content Provider?

A Content Provider is a component that allows applications to share data with other apps securely.

  • It manages access to a structured set of data (typically stored in an SQLite database).
  • Provides a standard interface to query, insert, update, or delete data using URIs.
  • Ensures data encapsulation and enforces permissions for accessing or modifying data.
  • Examples: Contacts Provider, Media Store.
  • Content Providers are useful for sharing data between apps or providing a unified interface within your app.

11. What is MVVM?

MVVM (Model-View-ViewModel) is a design pattern that separates concerns into three components:

  • Model: Represents data and business logic (repositories, databases, network).
  • View: UI layer (Activity/Fragment) that displays data and receives user inputs.
  • ViewModel: Acts as a bridge that exposes data to the View and handles user actions by requesting data updates from the Model.

Benefits:

  • Decouples UI from business logic.
  • Improves testability and maintainability.
  • Works well with LiveData and data binding.

12. What’s the role of Repository in Clean Architecture?

The Repository acts as a mediator between data sources and the domain/business layer.

  • It abstracts data operations and decides whether to fetch data from a local database, cache, or remote server.
  • Provides a clean API for use cases or ViewModels to access data.
  • Keeps the domain layer unaware of the underlying data source implementation.
  • Helps achieve separation of concerns and facilitates easier testing.

13. How do you manage dependency injection in Android?

Dependency Injection (DI) is a design pattern that allows an object to receive other objects it depends on.

In Android, DI improves modularity and testability by decoupling object creation from usage.

Common DI frameworks:

  • Dagger: A fully static, compile-time DI framework, widely used but with a steep learning curve.
  • Hilt: Built on Dagger but with simplified setup and Android-specific integrations (recommended by Google).
  • Koin: A Kotlin-based DSL for DI, easier to learn but with runtime injection.

DI helps manage dependencies like ViewModels, Repositories, Services, avoiding manual object creation.


14. What is Hilt and how is it different from Dagger?

  • Dagger: A static dependency injection framework that requires manual setup and boilerplate (components, modules, scopes). Powerful but complex.
  • Hilt:
    Built on top of Dagger, Hilt simplifies DI setup with standard components and annotations tailored for Android apps.

Advantages of Hilt:

  • Provides pre-defined components for Android lifecycle (Activity, Fragment, ViewModel).
  • Simplifies scoping and component management.
  • Integrates easily with Jetpack components.
  • Reduces boilerplate code compared to plain Dagger.

15. What is a Use Case (Interactor) in Clean Architecture?

  • A Use Case (sometimes called Interactor) encapsulates a single, specific business action or feature of the application.
  • It represents the application-specific business logic (e.g., “Get User Profile” or “Place Order”).
  • Use Cases interact with repositories and models to fetch or update data and prepare it for the presentation layer.
  • Keeps business logic separate from UI and data layers.
  • Makes code more modular, easier to maintain, and test.

 

16. What is LiveData in Android?

LiveData is an observable data holder class that is lifecycle-aware. This means it respects the lifecycle state of app components such as activities, fragments, or services. Observers are notified of changes only when they are in an active lifecycle state (started or resumed). This helps avoid crashes or memory leaks that can occur when UI components try to update themselves when not active. LiveData also automatically manages observers' subscription and unsubscription, reducing boilerplate code. It integrates well with ViewModel and data-binding components for reactive UI updates.


17. Explain Android Data Binding.

Android Data Binding is a library that allows you to bind UI components in XML layouts directly to data sources, typically observable data models or ViewModels. This reduces boilerplate code like findViewById and manual UI updates. Using data binding, the UI automatically updates when the underlying data changes. It supports two-way binding, enabling data changes to flow both from UI to data and vice versa. Data Binding also supports binding expressions, allowing logic in XML layouts for conditional formatting or method calls.


18. What is the difference between Serializable and Parcelable?

Both Serializable and Parcelable are used to pass objects between activities or fragments by serializing the object’s state.

  • Serializable is a standard Java interface that uses reflection and is easier to implement but is slower and causes more garbage collection.
  • Parcelable is Android’s recommended way; it’s faster because it requires you to explicitly write how data should be flattened/unflattened, which reduces overhead. Parcelable is more complex but preferred for performance-sensitive applications.

19. What is a BroadcastReceiver?

A BroadcastReceiver is an Android component that listens for system-wide or app-wide broadcast messages (intents). These broadcasts can be about system events such as battery low, network connectivity changes, or custom app events. BroadcastReceivers allow apps to react to these events, e.g., starting a service when the device boots. They can be registered statically in the manifest or dynamically at runtime. From Android 8 (Oreo), background execution limits restrict implicit broadcasts, so you may need to use JobScheduler or WorkManager instead.


20. What is WorkManager?

WorkManager is a Jetpack library designed for deferrable, guaranteed background work that needs to be executed even if the app exits or device restarts. It supports constraints like network availability, charging status, and provides an easy API to schedule one-time or periodic tasks. WorkManager handles API differences internally, using JobScheduler, Firebase JobDispatcher, or AlarmManager as appropriate. It is lifecycle-aware and integrates well with LiveData and ViewModels.


21. Explain different types of Android storage options.

Android offers several storage options to save data depending on needs:

  • SharedPreferences: Stores key-value pairs of primitive data, used for small configuration or settings data. Data is private to the app.
  • Internal Storage: Private storage space on the device where files are saved and accessible only by the app.
  • External Storage: Shared storage that can be accessed by other apps or users. Requires runtime permissions (like storage permission).
  • SQLite Database: Structured data stored in a local database for complex queries and relationships.
  • Room Database: A Jetpack library abstraction over SQLite providing compile-time checks, easier queries, and migrations.
  • Content Providers: For sharing data across apps.

22. What are the key lifecycle methods of an Activity?

The primary lifecycle callbacks include:

  • onCreate(): Initialization, set up UI.
  • onStart(): Activity is becoming visible.
  • onResume(): Activity is interacting with the user.
  • onPause(): Activity losing focus but still visible.
  • onStop(): Activity is no longer visible.
  • onDestroy(): Cleanup before destruction.
  • onRestart(): Called after onStop(), before onStart() if activity is restarting.

Understanding these helps properly manage resources and state during app use.


23. What is a Handler and Looper in Android?

  • Handler: Allows communication with a thread’s message queue. It posts runnable tasks or messages to be executed on the thread's Looper.
  • Looper: Runs in a thread to manage the message queue. The main UI thread has a Looper by default to process UI events.

Together, they enable message-driven asynchronous processing on threads, often used to update UI from background threads.


24. Explain Coroutines in Kotlin and their advantages.

Kotlin Coroutines provide a simplified way to handle asynchronous programming by writing sequential, non-blocking code. They allow suspending functions that can pause execution without blocking the thread, resuming later. Advantages include:

  • Simplified concurrency model
  • Less callback hell compared to traditional async APIs
  • Structured concurrency for better control
  • Integration with Android lifecycle for safe async operations.

25. How does Retrofit work in Android?

Retrofit is a type-safe HTTP client for Android and Java that simplifies network calls. You define REST API endpoints as interfaces with annotations for HTTP methods. Retrofit handles creating HTTP requests, parsing JSON/XML responses (via converters like Gson), and returns typed objects. It supports synchronous and asynchronous calls and integrates with coroutines for suspending functions.


No comments:

Post a Comment

Top 30 interview questions

Android Development Interview Q&A (5+ Years Experience) Android Development Fundamentals Q: What are the main Android app components and...