Interview Questions


Java - Empty Collections

When a collection is returned from a method, and if the collection to be returned is null , then it is always better to return an empty collection.
There are methods available in Collections class to return empty collection with respect to Map, List and Set.

public class Matrix<T>
    public static void main(String[] input1)
        //Write code here
        Matrix<String> mt = new Matrix<String>();
        Collection<String> lstStr = mt.processColl(null);
    public Collection<T> processColl(Collection<T> coll)
        if(null == coll)

Java - Array Blocking Queue

Queue is used in places where one or more threads pass information or data to other threads. Basically they are used to transfer data between threads.

Queue typically orders elements in First in First out (FIFO) order. In FIFO, all new elements are added at the tail or bottom of the Queue. While retrieving data from the Queue, the element which is inserted first will be retrieved first.

Queue offers functionalities like data insertion and data retrieval.

In this article, let us see a simple example of using Array Blocking Queue.

Example :

Java Comparator and Comparable Interface

In this section lets discuss about comparator and comparable interface in java.

Consider a scenario where you want to sort a collection for eg. Arraylist of type Strings. It can be done using Collections.sort(..) method. But how exactly the strings are getting sorted?

Lets take a closer look in to the Collections.sort() method. It is a static method taking a List as input parameter. The list elements should implement comparable interface. So what are the methods in comparable interface?

public interface Comparable<T>
    public int compareTo(T o);

Java Object Pooling

In this section let us discuss about object pooling in java. In the world of java programming, creation of any new object may sound costly depending on the nature of object and the resources it uses. It would be better if we can pool those objects and reuse the same. Creation should be done only when it is necessary. There may also be need to create only a fixed number of objects and reuse them without creating new objects more than the fixed limit.

Java Search Objects

Consider a scenario where you need to search through a list of objects for a particular value. For example, There is an ArrayList of Employees as follows.

    ArrayList al = new ArrayList();
    al.add(new Employee("100", "jack"));
    al.add(new Employee("102", "rose"));
    al.add(new Employee("103", "rock"));
    al.add(new Employee("104", "lily"));