Array, ArrayList (Interview Questions)
- How can you compare arrays for equality?
Answer:
You can compare arrays for equality with the convenience method Java.util.Arrays.equals()
- What is the purpose of cloning in object-oriented programming?
Answer:
The purpose of cloning is to create an independent copy of an object.
- What must you do to create a "deep copy" in Java?
Answer:
To make a deep copy you must create cloned copies of all components (excluding Strings and primitive values) by invoking their respective clone methods.
- What type of Exception class is CloneNotSupportedException?
Answer:
It is a checked exception class.
- What are two different ways to view an array?
Answer:
1. You can view it as a collection of individual indexed variables like
temp[0],..., temp[n]
2. You can view an array as one large composite object that has a number of different values all of the same type
- What is the difference between String and Stringbuffer?
Answer:
The String Buffer class represents a mutable string of characters that
can grow or shrink as necessary. Its mutability makes it suitable for processing text in place which is not possible with the immutable String class.
- What does "protected" visibility for 1) fields and 2) methods allow?
Answer:
Because it is fairly common for a subclass method to reference data fields in its superclass, Java provides a less restrictive form of visibility called protected visibility. A data field (or method) with protected visibility can be accessed in either the class defining it, in any subclass of that class, or any class in the same package.
- When would you advise to use an ArrayList?
Answer:
ArrayList objects are used most often when a programmer wants to be able to add new elements to the end of a list but still needs the capability to
access the elements stored in the list in arbitrary order. The ArrayList class implements a growable array of objects.
- What is IllegalMonitorStateException?
Answer:
This signals an illegal monitor state. It is thrown by the Object notify() and wait() methods used for thread synchronization.
- What is the advantage of "ArrayList" over Vector?
Answer: ArrayList is like Vector without the synchronization overhead.
Vector and Stack are legacy implementations left over from Java 1.0