Android Interview Questions Part 1
I am going to share my experiences for an android interview for various companies.
- Shared preference method Apply() vs Commit()
Apply():
This is for asynchronous operation.
It doesn't return any value so we can’t say whether the operation is successful or fail.
It is faster.
Commit():
This is for synchronous operation on the main thread.
It returns true/false for the operation is successful or fail.
It is slower as compared to apply().
2. When does onSavedInstanceState() gets called?
This method is called before onStop() but there is no guarantee that it will be. called before or after onPause() method.
When we press the Home button and app goes in background.
When device orientation gets changed
When the user presses the back button then this method will not be called.
3. How to make ArrayList immutable?
final List<String> myList = new ArrayList<String>();
This statement will make the myList variable as final. But still, we can access particular index value and can change those values as well.
So how we can do that……..
1. Make list as unmodifiable by using
Collections.unmodifiableList(myList);
or there is one more way to make this immutable
example :::
public class Demo{ final private ArrayList<String> list; public Demo(ArrayList<String> listarg){
list = Collections.unmodifiableList(listarg); // this is 1 way
}
//return new Arraylist with same content as list
public ArrayList<String> getList(){
return new ArrayList<String>(list);
}
}
4. What is the relation between minSdk, targetSdk, compilerSdk??
minSdk ≤ TargetSdk ≤ compiledSdk
Target SDK: let suppose target SDK is 22 then the application doesn’t ask runtime permission which is from 23.
Compiled SDK: the version that Android build tool to use to compile & build an application in order to run, release and debug
5. Give any class example in android that uses a Singleton design pattern??
Although there may be many classes, I gave LocalBroadCastManager class example.
LocalBroadCastManager.getInstance(Context context)
so this class based on the singleton design pattern
some of them will try to confuse with Calender.getInstance() but in this case getInstance() is a static method of Calendar class.
6. Does Dagger allow partial Injection?
@Component(modules = A)
interface ABC{
inject(SampleActivtiy sample)
}@Component(modules = X)
interface XYZ{
inject(SampleActivtiy sample)
}class SampleActivity{
@Inject
A a;@Inject
X x;oncreate(Bundle){
ABC.inject(this);
XYZ.inject(this);
}
Will this compile? or it will throw an error?
No, Dagger doesn’t allow partial injection. We can use the subcomponent annotation. There should be only 1 parent. component. The rest can be subcomponent.
7. Why the inner class can access only final variable?
An anonymous inner class uses local variables by creating a private instance field which holds a copy of the value of the local variable.
The inner class isn’t actually using the local variable, but a copy.
It should be fairly obvious at this point that a “Bad Thing”™ can happen if either the original value or the copied value changes; there will be some unexpected data synchronization problems.
In order to prevent this kind of problem, Java requires you to mark local variables that will be used by the anonymous inner class as final (i.e., unchangeable). This guarantees that the inner class’ copies of local variables will always match the actual values.
Soon I will post more questions on android, core java. wait for the next part…
till then happy learning… :)