Here are Part 2 of 50
important interview questions and answers for a fresher Android
developer role, covering fundamentals of Android, Java/Kotlin, and some
common development practices.
26. What is the Elvis operator in Kotlin?
The Elvis
operator (?:) is a shorthand for handling nullable
expressions. It returns the left-hand side if it's not null; otherwise, it
returns the right-hand side.
Example:
kotlin
CopyEdit
val length = name?.length ?: 0
Here, if name is null, length will be 0 instead of throwing a
NullPointerException.
27. What is the difference between == and === in Kotlin?
- == (structural equality): Checks if two objects have equal
values (similar to equals() in Java).
- === (referential equality): Checks if two references point to
the exact same object in memory.
Example:
kotlin
CopyEdit
val a = "hello"
val b = "hello"
println(a == b) //
true (values are equal)
println(a === b) // might be true or false depending on
reference
28. What are extension functions?
Extension
functions allow adding new functions to existing classes without modifying
their source code or using inheritance.
Example:
kotlin
CopyEdit
fun String.addExclamation(): String {
return this +
"!"
}
val text = "Hello".addExclamation() // "Hello!"
They make code
more readable and modular.
29. What is a sealed class?
A sealed
class restricts class inheritance. All subclasses must be declared within
the same file. This helps represent restricted class hierarchies and enables
exhaustive when expressions.
Example:
kotlin
CopyEdit
sealed class Result {
class
Success(val data: String) : Result()
class Error(val
error: Exception) : Result()
}
Useful for
representing states like loading, success, and error.
30. What is the difference between interface and abstract
class?
|
Aspect |
Interface |
Abstract Class |
|
Purpose |
Defines a
contract with abstract methods (no state) |
Can provide
partial implementation and state |
|
Multiple
Inheritance |
Supports
multiple interfaces |
Single
inheritance only |
|
Methods |
Only abstract
methods and default implementations (Kotlin allows default) |
Can have
abstract and concrete methods |
|
Fields |
Cannot have
instance fields (only constants) |
Can have
instance fields |
Use interfaces for behavior contracts and abstract classes when sharing code among related classes.
31. What is a thread in Android?
A thread
is a separate path of execution within a program. Android apps run on a main
thread (UI thread) responsible for handling UI updates and user interactions.
If long-running operations like network calls or heavy calculations run on the
main thread, the app freezes or becomes unresponsive.
To avoid this,
background threads are used for such tasks, keeping the UI smooth.
32. What is the main thread or UI thread?
The main
thread or UI thread is the thread where Android UI components run.
It handles all user interface events like clicks, drawing views, and updating
UI elements. It’s important not to block this thread with heavy operations, or
the app will lag or trigger an "Application Not Responding" (ANR)
error.
33. What is AsyncTask?
AsyncTask was an Android class used to perform
background operations and update the UI thread without manual thread handling.
It provided three main methods:
- doInBackground() — Runs in the background thread.
- onPreExecute() — Runs on UI thread before
background work starts.
- onPostExecute() — Runs on UI thread after
background work finishes.
Note: AsyncTask is deprecated in modern
Android development, replaced by Kotlin coroutines or libraries like
WorkManager.
34. What is Handler in Android?
A Handler
allows you to send and process Runnable objects or messages on a thread’s
message queue. It’s commonly used to post tasks to the main thread from a
background thread, or schedule delayed tasks.
Example:
java
CopyEdit
Handler handler = new Handler(Looper.getMainLooper());
handler.post(() -> {
// Code to run
on the main thread
});
35. What are Kotlin coroutines?
Kotlin
coroutines provide a
simple and efficient way to write asynchronous code. They allow you to write
sequential code that’s non-blocking and lightweight. Coroutines help avoid
callback hell and simplify threading.
Example:
kotlin
CopyEdit
GlobalScope.launch(Dispatchers.Main) {
val data =
withContext(Dispatchers.IO) { fetchData() }
updateUI(data)
}
36. What is LiveData?
LiveData is an observable data holder class
that respects the Android lifecycle. It updates observers (usually UI
components) only when they are in an active lifecycle state (e.g., started or
resumed). This helps avoid memory leaks and crashes caused by updates when the
UI is not visible.
37. What is ViewModel?
A ViewModel
is part of Android’s architecture components that stores and manages UI-related
data in a lifecycle-conscious way. It survives configuration changes like
screen rotations, ensuring data persists without reloads.
You use
ViewModel to separate UI data from UI controllers (Activities/Fragments),
improving app design.
38. What is Data Binding in Android?
Data Binding is a technique that binds UI
components in layouts to data sources in your app, typically using XML. It
reduces boilerplate code like findViewById() and enables automatic UI updates when
data changes.
Example of
binding a text value in XML:
xml
CopyEdit
<TextView android:text="@{user.name}" />
39. What is Dependency Injection?
Dependency
Injection (DI) is a design
pattern used to achieve loose coupling by providing dependencies from outside a
class rather than creating them inside it. It improves testability and
modularity.
In Android,
libraries like Dagger and Hilt automate DI.
40. What is ProGuard?
ProGuard is a tool that shrinks, optimizes, and
obfuscates your code by removing unused code and renaming classes/methods to
short names, making it harder to reverse engineer the app.
It reduces APK size and increases app security. ProGuard rules can be customized to keep essential classes intact.
41. What is Gradle in Android?
Gradle is the
build automation system used by Android Studio to compile, build, test, and
package your app. It manages dependencies, build variants (like debug/release),
and supports custom build configurations.
Gradle scripts
(build.gradle) define how your app should be built,
which SDK versions to use, and what external libraries to include.
42. What is an APK?
APK (Android
Package Kit) is the file format used to distribute and install Android apps. It
contains all the app’s compiled code, resources, assets, and manifest file
packaged into one archive.
When you build
your app, Android Studio generates an APK that can be installed on devices.
43. What is an ANR in Android?
ANR stands for Application
Not Responding. It happens when the main thread is blocked for too long
(usually more than 5 seconds) without responding to user input, causing Android
to show a dialog to the user offering to close the app.
Common causes:
Long-running operations on the UI thread, infinite loops, or heavy computations
without background threading.
44. What is the difference between Serializable and Parcelable?
Both are used
to serialize objects to pass between Activities or store them.
- Serializable: Java interface, easier to
implement but slower and uses more memory because of reflection.
- Parcelable: Android-specific interface
designed for high-performance serialization. Requires manual
implementation but is faster and recommended for Android.
45. What is the role of onSaveInstanceState()?
onSaveInstanceState() is called before an Activity or
Fragment may be destroyed (e.g., on rotation). It allows you to save UI state
or temporary data in a Bundle so it can be restored later in onCreate() or onRestoreInstanceState().
Helps maintain
continuity of user experience during configuration changes.
46. What is a BroadcastReceiver?
A BroadcastReceiver
listens for system-wide or app-specific broadcast messages (Intents). It allows
apps to respond to events like battery low, network change, or custom
broadcasts.
Receivers can
be registered dynamically in code or statically in the manifest.
47. What is the difference between static and dynamic
BroadcastReceiver?
- Static Receiver: Declared in the manifest file;
receives broadcasts even when the app is not running (limited in Android
8.0+).
- Dynamic Receiver: Registered at runtime in code,
only active while the app or component is running.
48. What is SharedPreferences?
SharedPreferences is a simple way to store small
key-value pairs persistently, like user settings or preferences. Data is saved
in XML files and persists across app sessions.
Example:
java
CopyEdit
SharedPreferences prefs =
getSharedPreferences("MyPrefs", MODE_PRIVATE);
prefs.edit().putString("username",
"John").apply();
49. What is the difference between Serializable and Bundle?
- Serializable: Converts an object into a stream
of bytes for storage or transmission; can be slower.
- Bundle: A key-value map used to pass data
between activities/fragments. Bundles support only basic data types and
Parcelable objects.
Use Parcelable
objects with Bundles for performance.
50. What is a Loader in Android?
A Loader
asynchronously loads data in the background for an Activity or Fragment. It
automatically reconnects after configuration changes and manages data updates
efficiently.
Common example:
CursorLoader to load data from content providers.
No comments:
Post a Comment