Android Interview Questions Part 3

Kishan Maurya
5 min readMar 30, 2020

This is part 3 of the android interview experience. You can also read my previous articles on android interviews.

Android Interview Questions Part 1

Android Interview Questions Part 2

  1. What is the difference between Val, var, const Val?
  2. Internal keyword in kotlin?
  3. What is the ViewModel factory and constructor of view model?
  4. ViewModel with a single activity and multiple activities?
  5. Use of Mediator live data?
  6. MVVM vs MVP?
  7. Task affinity?
  8. Can GSP work without the internet?
  9. Lambda Expression Vs Anonymous class?
  10. Thread.sleep() & wait()?
  11. What are functional interfaces?

Recommended: Please solve it first, before moving on to the solution.

There can multiple more accurate answers to the question. Based on my assumptions and learning, I am writing my answers.

What is the difference between Val, var, const Val?

Var:
Kotlin’s keyword representing mutable, non-final variables. Once initialized, we’re free to mutate the data held by the variable.

Const are compile-time constants.
Meaning that their value has to be assigned during compile time, unlike vals, where it can be done at runtime.

Internal keyword in kotlin

The internal visibility modifier means that the member is visible within the same module.
More specifically, a module is a set of Kotlin files compiled together:
so the module can an IntelliJ IDEA module or a Maven project.

When dealing with multi-module projects in Android, internal is an extremely convenient modifier.
It makes things visible everywhere in the current module, but not visible outside.

What is the ViewModel factory and constructor of view model?

Implementations of ViewModelProviders.Factory interface is responsible to instantiate ViewModels. If you add an argument in the constructor you have to create your own implementation of ViewModelProvider.Factory to create your ViewModel instance.

ViewModelProviders.of() method internally creates default ViewModelProvider.Factory implementation for creating our ViewModel with no argument.
So when you add an argument in the constructor, the inner implementation of ViewModelProvider.Factory failed to initialize your ViewModel because of ViewModelProvider.Factory call the primary constructor for creating the ViewModel’s instance

If your ViewModel has dependencies and you want to test your ViewModel then you should create your own ViewModelProvider.Factory and passed dependency through ViewModel constructor and give value to the ViewModelProvider.Factory instance.

ViewModel with a single activity and multiple activities?

When you call ViewModelProviders.of(this), you actually create/retain a ViewModelStore which is bound to this, so different Activities have different ViewModelStore and
each ViewModelStore creates a different instance of a ViewModel using a given factory, so you can not have the same instance of a ViewModel in different ViewModelStores.

Use of Mediator live data?

MediatorLiveData is a subclass of LiveData that can observe other LiveData objects and react to OnChanged events from them.

For example, if you have a LiveData object in your UI that can be updated from a local database or a network, then you can add the following sources to the MediatorLiveData object:
A LiveData object associated with the data stored in the database.
A LiveData object associated with the data accessed from the network.

Your activity only needs to observe the MediatorLiveData object to receive updates from both sources.

MVVM vs MVP?

MVP :

For each Activity/Fragment (View) we require a Presenter. 
This is a hardbound rule.
The presenter holds the reference to the Activity and Activity Holds the reference to the presenter. 1:1 relationship and that's where the biggest issue lies.

MVVM:

Only View(Activity) holds the reference to ViewModel and not vice versa, 
this solves our tight coupling issue. A single view can hold a reference to multiple ViewModels.

Since Presenters are hardbound to Views, writing unit tests becomes slightly difficult as there is a dependency of a View.

ViewModels are even more Unit Test friendly as they just expose the state and hence can be independently tested without requiring the need for testing how data will be consumed, In short, there is no dependency of the View.

Task affinity

By default, all activities in your app have an affinity to the package name of your app.

If MyActivity have a different affinity, which in turn places it in a different stack we use a property called taskAffinity of the <activity> element

MyActivity is placed in a different stack named MyActivity.container and the other stack where the rest of the activities live points to the package name of the app.

If I launch MyActivity2 from my MyActivity, the former will be a part of MyActivity.container stack.

Can GSP work without the internet?

There is no internet connection required to use GPS services. Global positioning system (GPS) is available FREE of cost everywhere on earth. That’s the reason your car’s GPS can work even if there is no internet connectivity in car’s navigation system.

Lambda Expression Vs Anonymous class

In the anonymous classes, ‘this’ keyword resolves to the anonymous class itself,
whereas for lambda expression, ‘this’ keyword resolves to enclose class where lambda expression is written.

An anonymous class object generates a separate class file after compilation
Java compiler compiles lambda expressions and converts them into the private method of the class.

Anonymous classes can be used in case of more than one abstract method while a lambda expression specifically used for functional interfaces

Anonymous class is a class without a name. While lambda expression is a method without a name. (anonymous function).

Thread.sleep() & wait() method

The fundamental difference is that wait() is from Object and sleep() is a static method of Thread.

The major difference is that wait() releases the lock while sleep() doesn’t release any lock while waiting.

wait() is used for inter-thread communication while sleep() is used to introduce a pause on execution, generally.

wait() should be called from inside synchronize or else we get an IllegalMonitorStateException, while sleep() can be called anywhere.

To start a thread again from wait(), you have to call notify() or notifyAll().
As for sleep(), the thread gets started after a specified time interval.

What are functional interfaces?

A single method interface is also sometimes referred to as a functional interface. An interface is still a functional interface even if it contains the default and static methods, as long as the interface only contains a single unimplemented (abstract) method.

Photo by Helloquence on Unsplash

Thanks for reading. Soon I will post more articles on Android and core Java.
Till then happy learning and keep reading!

You could check out my other interesting topics here.

--

--