IGNOU BCA MCA Students - VIVA Question Answer for JAVA or JSP
JAVA or JSP Selected Question Answer - PART V
25. What differences exist between HashMap and Hashtable ? Both the HashMap and Hashtable classes implement the Map interface and thus, have very
similar characteristics. However, they differ in the following features:
·
A HashMap allows the existence of null keys and values, while a Hashtable doesn’t allow neither null keys, nor null values.
·
A Hashtable is synchronized, while a HashMap is not. Thus, HashMap is preferred in single-threaded environments, while a Hashtable is suitable for multi-threaded environments.
·
A HashMap provides its set of keys and a Java application can
iterate over them. Thus, a HashMap is fail-fast. On the other hand, a Hashtable provides an Enumeration of its keys.
26. What is difference between Array and ArrayList ? When will
you use Array over ArrayList ? The Arrayand ArrayList classes differ on the following features:
·
For a list of primitive data types, the collections use
autoboxing to reduce the coding effort. However, this approach makes them
slower when working on fixed size primitive data types.
27. What is difference between ArrayList and LinkedList ? Both the ArrayList and LinkedList classes implement the List interface, but they differ on
the following features:
·
An ArrayList is an index based data structure backed by an Array. It provides random access to its elements with a performance
equal to O(1). On the other hand, a LinkedList stores its data as list of elements and every element is
linked to its previous and next element. In this case, the search operation for
an element has execution time equal to O(n).
·
The Insertion, addition and removal operations of an element are
faster in a LinkedList compared to an ArrayList, because there is no need of resizing an array or updating the
index when an element is added in some arbitrary position inside the
collection.
·
A LinkedList consumes more memory than an ArrayList, because every node in a LinkedList stores two references, one for its previous element and
one for its next element.
28. What is Comparable and Comparator interface ? List their
differences. Java provides the Comparableinterface, which contains only one method, called compareTo. This method compares two objects, in order to impose an order
between them. Specifically, it returns a negative integer, zero, or a positive
integer to indicate that the input object is less than, equal or greater than
the existing object. Java provides the Comparator interface, which contains two methods, called compare and equals. The first method compares its two input arguments and imposes
an order between them. It returns a negative integer, zero, or a positive
integer to indicate that the first argument is less than, equal to, or greater
than the second. The second method requires an object as a parameter and aims
to decide whether the input object is equal to the comparator. The method
returns true, only if the specified object is also a comparator and it imposes
the same ordering as the comparator.
29. What is Java Priority Queue ? The PriorityQueue is an unbounded queue, based on a priority
heap and its elements are ordered in their natural order. At the time of its
creation, we can provide a Comparator that is responsible for ordering the
elements of the PriorityQueue. A PriorityQueue doesn’t allow null values, those objects that
doesn’t provide natural ordering, or those objects that don’t have any
comparator associated with them. Finally, the Java PriorityQueue is not thread-safe and it requires
O(log(n)) time for its enqueing and dequeing operations.
No comments:
Post a Comment