31. What is the tradeoff between using an unordered array versus
an ordered array ?
The major advantage of an ordered array is that the search
times have time complexity of O(log n), compared to that of an unordered array,
which is O (n). The disadvantage of an ordered array is that the insertion
operation has a time complexity of O(n), because the elements with higher
values must be moved to make room for the new element. Instead, the insertion
operation for an unordered array takes constant time of O(1).
32. What are some of the best practices relating to the Java
Collection framework ?
· Choosing the right type of the
collection to use, based on the application’s needs, is very crucial for its
performance. For example if the size of the elements is fixed and know a
priori, we shall use an Array,
instead of an ArrayList.
·
Some collection classes allow us to
specify their initial capacity. Thus, if we have an estimation on the number of
elements that will be stored, we can use it to avoid rehashing or resizing.
·
Always use Generics for type-safety,
readability, and robustness. Also, by using Generics you avoid theClassCastException during
runtime.
·
Use immutable classes provided by
the Java Development Kit (JDK) as a key in a Map, in order to avoid the
implementation of the hashCode and
equals methods for our custom class.
·
Program in terms of interface not
implementation.
·
Return zero-length collections or
arrays as opposed to returning a null in case the underlying collection is
actually empty.
33. What’s the difference between Enumeration and Iterator
interfaces ?
Enumeration is
twice as fast as compared to an Iterator and uses very less memory. However,
the Iterator is
much safer compared to Enumeration,
because other threads are not able to modify the collection object that is
currently traversed by the iterator. Also,Iteratorsallow
the caller to remove elements from the underlying collection, something which
is not possible withEnumerations.
34. What is the difference between HashSet and TreeSet ?
The HashSet is
Implemented using a hash table and thus, its elements are not ordered. The add,
remove, and contains methods of a HashSet have
constant time complexity O(1). On the other hand, a TreeSet is
implemented using a tree structure. The elements in a TreeSet are
sorted, and thus, the add, remove, and contains methods have time complexity of
O(logn).
Garbage
Collectors
35. What is the purpose of garbage collection in Java, and when
is it used ?
The purpose of garbage collection is to identify and
discard those objects that are no longer needed by the application, in order
for the resources to be reclaimed and reused.
36. What does System.gc() and Runtime.gc() methods do ?
These
methods can be used as a hint to the JVM, in order to start a garbage
collection. However, this it is up to the Java Virtual Machine (JVM) to start
the garbage collection immediately or later in time.
37. When is the finalize() called ? What is the purpose of
finalization ?
The finalize method is called by the garbage collector,
just before releasing the object’s memory. It is normally advised to release
resources held by the object inside the finalize method.
38. If an object reference is set to null, will the Garbage
Collector immediately free the memory held by that object ?
No,
the object will be available for garbage collection in the next cycle of the
garbage collector.
39. What is structure of Java Heap ? What is Perm Gen space in
Heap ?
The JVM has a heap that
is the runtime data area from which memory for all class instances and arrays
is allocated. It is created at the JVM start-up. Heap memory for objects is
reclaimed by an automatic memory management system which is known as a garbage
collector. Heap memory consists of live and dead objects. Live objects are
accessible by the application and will not be a subject of garbage collection.
Dead objects are those which will never be accessible by the application, but
have not been collected by the garbage collector yet. Such objects occupy the
heap memory space until they are eventually collected by the garbage collector.
40. What is the difference between Serial and Throughput Garbage
collector ?
The throughput garbage collector uses a parallel version
of the young generation collector and is meant to be used with applications
that have medium to large data sets. On the other hand, the serial collector is
usually adequate for most small applications (those requiring heaps of up to
approximately 100MB on modern processors).
No comments:
Post a Comment