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?
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?
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?
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?
- 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:
- Machine Learning – Training, Validation & Test Data Set - June 13, 2021
- Why use Random Seed in Machine Learning? - June 12, 2021
- Deep Learning – Top 5 Online Jupyter Notebooks Servers - June 11, 2021