I have been involved in many a java interviews and following are five tricky ones where I found several junior to mid-level Java developers faltering once in a while. Thus, I thought to put an article around these questions to help junior Java developers make familiar with these questions. The article presents the 5 tricky interview questions in relation with Java ArrayList, I believe, could get the interviewee score some browny points, if answered well. Let me know if you agree and would like to add another set of questions to the list below:
How does the size of Arraylist increases automatically? Could you share the code?This is the most trickiest one and many were unable to answer this one. The trick here is the fact that when someone tries to add an object to the arraylist, Java checks to ensure that there is enough capacity in the existing array to hold the new object. If not, a new array of a greater size is created, the old array is copied to new array using Arrays.copyOf and the new array is assigned to the existing array. Look at the code below (taken from Java ArrayList Code at GrepCode.com).
Pay attention to the fact that a new array is created; Objects from old array is copied to the new array and the new array is assigned to the existing array member variable.
When would you use ArrayList and when LinkedList?This is again one of the question where many of the interviewee got confused. Primarily, one would want to use ArrayList in the cases where there is a greater need to access the element rather than insertion or deletion. On the other hand, one would want to use the LinkedList when there is a greater need for insertion and deletion and not much use of the accessing the element at a particular index. This is mainly because the worst time complexity of accessing an element/object in an ArrayList is always “1″ whereas in LinkedList it can be “n”. In case of addition or deletion of element in an ArrayList, there is an operation such as System.arraycopy is involved. This is quite an expensive operation and thus, in usecases where frequent insertion and deletion of elements are involved, LinkedList is preferred where it is all about adding or removing the node and re-linking the existing node.
While passing an ArrayList to a method or returning an ArrayList from a method, when is it considered to be a security violation? How to fix this problem?When one passes the array to a method, if array is assigned to the member variable directly without making a copy of it, it could lead to a scenario that the original array when changed by the caller will also end up changing the array passed to the method. Look at the code below to see when it is security violation and subsequently how to fix the same.
Pay attention the fact that a copy of newMyArray is created and assigned to the member variable, myArray.
How do you copy one ArrayList to another? Could you share the code?Following are different techniques for copying an ArrayList object to another ArrayList:
Note that option 1 and 2 creates a shallow copy.
- Use clone() method such as following: ArrayList myObject = new ArrayList<Object>(myTempObject);
- Use ArrayList constructor object such as following:
- use Collection.copy method
How does the addition and deletion of an object at any index happens in ArrayList? Is it expensive? Explain?
In case of addition/deletion of elements in an ArrayList, there is an operation such as System.arraycopy is involved. This is quite an expensive operation and for usecases like these where frequent insertion and deletion are required, one may want to choose other Java collection such as LinkedList. Take a look at the code below:
Follow him on Twitter and Google+.
Latest posts by Ajitesh Kumar (see all)
- Learn R – How to Define Function in R - June 3, 2015
- Learn R – How to Create Multiple Density Plots using GGPlot - June 2, 2015
- Learn R – How to Create Density Plot over Histogram - June 2, 2015