Android’s most commonly asked interview questions.

Ekta Dass
7 min readSep 20, 2022

--

What is the difference between a val and const?

Both are immutable once the value is assigned to them we can not modify it. However, we have to assign the value to const during the initialization point and val during run time.

What is the output of the below code?

There will be a compile time error in this line “lateinit var string: String” because we can not assign null. We will see an error to remove the “initializers from the property”
Also, If the lateinit is not initialized then you will receive a UninitializedPropertyAccessException

How many ways we can bind data in MVVM?

enable data binding in build.gradle(:app)

We can do this by using data binding to bind data. Data bindings can both be OneWay or TwoWay to flow statistics from side to side between the View and ViewModel.
The Data Binding Library is a support library that allows you to bind UI components in your layouts to data sources in your app using a declarative format, rather than programmatically.

Advantages of using the Data Binding Library?

-> Reduce findViewById calls and enhance your app’s performance
-> Helps get rid of memory leaks or nullPointerExceptions
-> Uses declarative layout, which is more adaptable
-> Data and Views are separated from each other

What are the sealed classes?

Enum: allow a single instance of each value and can’t encode more information on each type. for eg. in an error case of success, or failure we can not have an associate exception.

Sealed class: like abstract classes, sealed classes allows us to represent hierarchies. The child class can be any type of class like a data class, an object, a regular class, or even
another sealed class. But we have to define hierarchies in the same class or nested classes. If we try to extend a sealed class outside the file cause compile time error.

How ViewModel retains the object?

There are various advantages of the ViewModel class such as handling configuration changes, data sharing, and lifecycle awareness to name a few.

But, how is ViewModel still alive when the activity itself is killed? (Check the link to deep dive)

It is ViewModelStore that stores the ViewModel and is retained when the rotation occurs and which returns the same view model instance in the new activity instance.

ViewModel store is linked to activity/fragment and when in the process of rotation current instance is destroyed and the new instance is created

ViewModelStoreOwner : it is an interface.It has just one method which returns the ViewModelStore.

By default Activity and Fragment are always ViewModel store owner because their parent classes in Android SDK implement the ViewModelStoreOwner interface. (ComponentActivity.java and Fragment.java )

Responsibilities of Viewmodel.

ViewModel is the primary factor of MVVM software. The number one responsibility of ViewModel is to offer data to the view, so that view can put that records on the display screen.
ViewModel provides loose coupling because ViewModel doesn’t references about the views but the views have references about ViewModel which is why there is a loose coupling. views keep observing the viewmodel using live data observable pattern and it depends upon views which type of data they want to fetch. because of loose coupling, it is easy to write unit test cases as well.

What is coroutine in Kotlin?

Coroutine is nothing but a lightweight thread that executes code asynchronously. They simply simplify asynchronous programming. It is used to manage long-running operations in the background to avoid blocking the main i.e UI thread. It is recommended because of its cheap, lightweight, and built-in cancellation properties.

Suspend function

We just need to add suspend modifier to tell the kotlin compiler that this function needs to be executed within the coroutine. The kotlin compiler writes the callbacks to show data under the hood for us when the computation can suspend. A suspend function must be called from another suspend function. When a suspend function returns it means it has completed all work.

In the above example, the network request is nothing but just another suspend function as you can see below and it returns data as the synchronous version of it.

withContext used to switch between Dispatchers nothing just a suspend function from the coroutine that takes dispatchers as parameters.

Dispatcher is nothing but just to run a task on a particular thread such as IO, Main, or Default dispatchers.

What happens if I launch the coroutine in this way:- There will a compiler error on launch that is because the launch must be called within a scope.

CoroutineScope

  • Keep track of coroutines
  • Ability to cancel them
  • Is notified of the failure
1st way
2nd way with job

Job is going to define the lifecycle of either the scope and also the coroutines. Whenever we are passing a job to a scope that means it is going to handle exceptions in a specific way.

Scope with a job: When a child fails, it propagates the cancellation to the other children. When the failure is notified, the scope will cancel itself and propagates the execution up. ( crash the app in case of UI-related scope)

Scope with a SupervisorJob: The failure of the child doesn’t affect the other children. When the failure is notified the scope doesn't do anything.

Difference between launch and async in coroutine?

Both create a new coroutine. With the launch, we don't receive any value.

With async, we get a return value. It returns a deferred object and we can call await with the object. await will suspend the execution of the coroutine until async finished its work and it will return the value of the coroutine

launch throws the exception and async hold on to an exception until await is called.

async example

Difference between Hilt vs Dagger?

Dagger creates a graph to find where to get dependencies when needed in your project. For Dagger to do this we need to add the @Component annotation.

In Dagger-Android, we have to create a component class with a builder/factory, including every module and we should inject the application context into the Application class after building our project. Here is the Dagger-Android way to construct a component.

However, we do not need to create this component in Hilt. Hilt already does this for us when we add the @HiltAndroidApp annotation. Also, the instance of the application can be @Injected by Hilt into other modules.

But in Hilt, we don’t need to create a component, include every module, and build for generating DaggerAppComponent class.

In Dagger, we create scope annotations such as ActivityScope, and FragmentScope to specify the lifecycle, but hilt provides us with core components such as Application, Activity, Fragment, Service, and View.

In Dagger, @ContributesAndroidInjector is used to automatically generate the codes of the subcomponents and remove the common codes. This causes us to write too many @Scope annotations and @Inject annotations in large projects.

However, we do not need to write these annotations in Hilt. Instead, Hilt provides the @AndroidEntryPoint annotation. In this way, instances are @Injected into the required fields by Hilt.

Difference between Room vs SQlite?

In the case of SQLite, There is no compile time verification of raw SQLite queries. But in Room, there is SQL validation at compile time.
As your schema changes, you need to update the affected SQL queries manually. Room solves this problem. You need to use lots of boilerplate code to convert between SQL queries and Java data objects. But, Room maps our database objects to Java Object without boilerplate code. Room is built to work with LiveData and RxJava for data observation, while SQLite does not.
Room annotations and main components:

@Entity — Define our database tables
@DAO — Provide an API for reading and writing data
@Database — Represent a database holder

What is Google analytics?

Google App Engine is a cloud-based computing service developed to host web applications in the existing Google infrastructure. It is offered as a Platform as a Service (PaaS) product that gives Web app developers and enterprises access to Google’s scalable hosting and tier 1 Internet service. Google App Engine boasts scalability and easy management as well as free use of predefined consumed resources and comes with Java support.

What is Jetpack Compose?

Jetpack Compose is a modern toolkit for building native Android UI. Jetpack Compose simplifies and accelerates UI development on Android with less code, powerful tools, and intuitive Kotlin APIs.

You won’t be editing any XML layouts or using the Layout Editor. Instead, you will call composable functions to define what elements you want, and the Compose compiler will do the rest.

Will post more questions later.. Good Luck !! :)

--

--