Java Interview Questions | Page 5

Question 41:

Can the Garbage Collection be forced by any means?

No. The Garbage Collection can not be forced, though there are few ways by which it can be requested there is no guarantee that these requests will be taken care of by JVM.

Question 42:

When does an object become eligible for garbage collection?

An object becomes eligible for Garbage Collection when no live thread can access it.

Question 43:

Is garbage collector a dameon thread?

Yes, GC is a dameon thread. A dameon thread runs behind the application. It is started by JVM. The thread stops when all non-dameon threads stop.

Question 44:

What is package?

A package is a group of similar type of classes interfaces and sub-packages. It provides access protection and removes naming collision.

Question 45:

What happens when start() is called?

A new thread of execution with a new call stack starts. The state of thread changes from new to runnable. When the thread gets chance to execute its target run() method starts to run.

Question 46:

What is a volatile keyword?

In general each thread has its own copy of variable, such that one thread is not concerned with the value of same variable in the other thread. But sometime this may not be the case. Consider a scenario in which the count variable is holding the number of times a method is called for a given class irrespective of any thread calling, in this case irrespective of thread access the count has to be increased so the count variable is declared as volatile. The copy of volatile variable is stored in the main memory, so every time a thread access the variable even for reading purpose the local copy is updated each time from the main memory. The volatile variable also have performance issues.

The volatile keyword guarantees more than just the reading and writing of a variable from and to main memory. Actually, the volatile keyword guarantees - If Thread A writes to a volatile variable and Thread B subsequently reads the same volatile variable, then all variables visible to Thread A before writing the volatile variable, will also be visible to Thread B.

Question 47:

What is the difference between yield() and sleep()?

yield() allows the current thread to release its lock from the object and scheduler gives the lock of the object to the other thread with same priority.
sleep() allows the thread to go to sleep state for x milliseconds. When a thread goes into sleep state it doesn’t releases the lock.

Question 48:

How can the Garbage Collection be requested?

There are two ways in which we can request the jvm to execute the Garbage Collection:-

The methods to perform the garbage collections are present in the Runtime class provided by java. The Runtime class is a Singleton for each java main program. The method getRuntime() returns a singleton instance of the Runtime class. The method gc() can be invoked using this instance of Runtime to request the garbage collection.
Call the System class System.gc() method which will request the jvm to perform GC.

Question 49:

What are the advantages of immutability?

Immutable objects are automatically thread-safe, the overhead caused due to use of synchronisation is avoided.
Once created the state of the immutable object can not be changed so there is no possibility of them getting into an inconsistent state.
The references to the immutable objects can be easily shared or cached without having to copy or clone them as there state can not be changed ever after construction.
The best use of the immutable objects is as the keys of a map.

Question 50:

How to sort list in reverse order?

To sort the elements of the List in the reverse natural order of the strings, get a reverse Comparator from the Collections class with reverseOrder(). Then, pass the reverse Comparator to the sort() method.

List list = new ArrayList();
Comparator comp = Collections.reverseOrder();
Collections.sort(list, comp)
Share interview questions


comments powered by Disqus


Social Media