Skip to main content

Android Job Interview Questions



In this post, I tried to wrote some Job interview questions, that I met during my Job interview journey. Yeah, some of the them are still so fresh, because I mess up them a lot. However, A sketch notebook is always better than a good memory, especially in the situation of the lack practice exercise.

finalize function

The finalize method is used in JVM’s GC collection. But, it was said that it never guaranteed to be called during the collection, that’s means unpredictable, so I guess why I should put the resource release code into an unpredictable method, still be puzzled.

How to write a singleton and thread-safe class?

Following is an ordinary Singleton class without thread-safe features.


public class Singular {
    private static Singular sInstance;

    private Singular () {
    }

    public static Singular getInstance() {
        if (sInstance == null) {
            sInstance = new Singular();
        }

        return sInstance;
    }
}


Following is a thread-safe version.

public class Singular {
    private static volatile Singular sInstance;

    private Singular () {
    }

    public static Singular getInstance() {
        if (sInstance == null) {
            synchronized (Singular.class) {
                if (sInstance == null) {
                    sInstance = new Singular();
                }
            }
        }

        return sInstance;
    }
}

Really, it is already in the singleton mode, that means it can only be instanced once, then why not instance it in the single thread environment at the beginning context initialization for instance.

HashMap vs ArrayMap vs SpareArray

The document said ArrayMap maybe more efficient than HashMap, but I don’t think so.
what HashMap is?
The HashMap is a combination of Hash function, index container(maybe an array) and multiple list container referred to every index to store the key/value component. The HashMap is fast because a good Hash function can map the key/value component to spare hash index, the lookup performance was excellent[O(1)].
What is ArrayMap or SparedArray?
There is not Hash function compared with HashMap, just two sorted arrays, one for key container, another for value container.



== .vs. equals

Basically, == compares if two objects have the same reference on the heap, so unless two references are linked to the same object, this comparison will be false.
The equals() is a method inherited from Object class. This method by default compares if two objects have the same reference. However, if you want to establish equality between two objects of the same class you should override this method. It is also very important to override the method hashCode() if you have overridden equals(). The result returned from the hashCode() method for two objects must be the same if their equals methods show that they are equivalent. The converse is not necessarily true.

Android Memory Garbage Collection(GC) and its debugging methods

There are a few programming conversions to avoid unCollectable Memory in JVM, such as:
1. Static member value references to the nonstatic internal class instance.
2. Static member value references to the Android’s API components.
3. Always remember to use context in Application as much as possible, avoid to save context variable of Activity if unnecessary.
4. Internal class reference to the external class’s instance uses WeakReference as much as possible.
5. The un-closed resource object like database.

ListView vs RecyclerView

Always RecyclerView.

Android LifeCycle methods

I forget the onRestart method, but I still believe it is useless.

Android Service

1. There are two ways to start Service. startService and bindService.
2. Service actually running at the main-UI thread. So to do time-consuming tasks, another work thread needs.
3. Service can be destroyed by itself(stopSelf) and in Context(stopService), then its memory canbe collected, also in low-memory condition, the Service can also be stopped and memory collected by JVM.
4. Service canbe runing in another process, by configure its android:process label.
5. There are some configurations here: android:export, android:permission, android:enabled.

Java Internal Class

There are static and non-Static internal Classes. Static one can be instanced by outside class, and can not references any non-static external class members.

Java Thread

1. check thread’s status:
isalive(), isDaemon(), currentThread(), getName(), …
2. Thread safe container at java.util.concurrent package(HashMap, List, Queue).
ConcurrentHashMap, Queue: LinkedList, PriorityQueue, ConcurrentLinkedQueue, BlockingQueue, LinkedBlockingQueue, ArrayBlockingQueue, PriorityBlockingQueue, DelayQueue, Deque
3. Decent ways to stop threads.
A thread can stop itself after return from Runnable, there is a deprecated stop() method to kill a thread, do not use stop method anymore, the much decent way is to use interrupt method to send a Interrupt Exception to the thread, and try to catch Interrupt Exception in the Runable run method, return and finish itself.

Java Exception

There are three kinds of Exceptions in Java: Error, Runtime Exception and Normal Exception.
1. Error can not be catched by code, it is generated by JVM.
2. Runtime Execption is not need to be catched by the code, because it is related to the code’s runtime environment.
3. Normal Exception is defined, throwed and catched in the code by ourself.

Four different kinds of Java Object References

Strong, Soft, Weak, Phantom are four kinds of object references in Java, the ordinary object is Strong type, which can not be garbage collected if it still have the not null reference. SoftReference is useful in caching container, it should be garbage collected if in lower memory condition. WeakReference is much more easier to be collected even not in lower memory condition. So the relation is: Strong > Soft > Weak > Phantom.


Comments

Popular posts from this blog

Bluedroid stack in android

How to setup a NAT server?

Network programming in elisp