Thursday, July 17, 2008

JAVA-UTIL PACKAGE

Util Collections

1) What is Collection API ?

The Collection API is a set of classes and interfaces that support operation on collections of objects. These classes and interfaces are more flexible, more powerful, and more regular than the vectors, arrays, and hashtables if effectively replaces.

Example of classes: HashSet, HashMap, ArrayList, LinkedList, TreeSet and TreeMap.
Example of interfaces: Collection, Set, List and Map.

-------------------------------------------------------------------------------------------------
2) Is Iterator a Class or Interface? What is its use?

Iterator is an interface which is used to step through the elements of a Collection.

-------------------------------------------------------------------------------------------------
3) What is the Vector class?

The Vector class provides the capability to implement a growable array of objects.

-------------------------------------------------------------------------------------------------
4) What's the main difference between a Vector and an ArrayList?

Java Vector class is internally synchronized and ArrayList isnot.

--------------------------------------------------------------------------------------------------
5) What is the Properties class?

The properties class is a subclass of Hashtable that can be read from or written to a stream. It also provides the capability to specify a set of default values to be used.

--------------------------------------------------------------------------------------------------
6) What is the Map interface?

The Map interface replaces the JDK 1.1 Dictionary class and is used associate keys with values.

-------------------------------------------------------------------------------------------------
7) What are Vector, Hashtable, LinkedList and Enumeration?

Vector : The Vector class provides the capability to implement a growable array of objects.

Hashtable : The Hashtable class implements a Hashtable data structure. A Hashtable indexes and stores objects in a dictionary using hash codes as the object's keys. Hash codes are integer values that identify objects.

LinkedList: Removing or inserting elements in the middle of an array can be done using LinkedList. A LinkedList stores each object in a separate link whereas an array stores object references in consecutive Locations.

Enumeration: An object that implements the Enumeration interface generates a series of elements, one at a time. It has two methods, namely hasMoreElements( ) and nextElement( ). HasMoreElemnts( ) tests if this enumeration has more elements and nextElement method returns successive elements of the series.

-------------------------------------------------------------------------------------------------
8) What is the difference between set and list?

Set stores elements in an unordered way but do not contain duplicate elements, whereas list stores elements in an ordered way but may contain duplicate elements.

-------------------------------------------------------------------------------------------------
9).What is the List interface?

The List interface provides support for ordered collections of objects.

-------------------------------------------------------------------------------------------------
10). What is the Vector class?

The Vector class provides the capability to implement a growable array of objects.

-------------------------------------------------------------------------------------------------

11). What is an Iterator interface?

The Iterator interface is used to step through the elements of a Collection.

------------------------------------------------------------------------------------------------

12). What is the GregorianCalendar class?

The GregorianCalendar provides support for traditional Western calendars.

-------------------------------------------------------------------------------------------------

13). What is the SimpleTimeZone class?

The SimpleTimeZone class provides support for a Gregorian calendar.

-------------------------------------------------------------------------------------------------
14). What is the Properties class?

The properties class is a subclass of Hashtable that can be read from or written to a stream. It also provides the capability to specify a set of default values to be used.

-------------------------------------------------------------------------------------------------
15). What is the Locale class?

The Locale class is used to tailor program output to the conventions of a particular geographic, political, or cultural region.

-------------------------------------------------------------------------------------------------
16). What is the Map interface?

The Map interface replaces the JDK 1.1 Dictionary class and is used associate keys with values.

-------------------------------------------------------------------------------------------------
17). What is the Set interface?

The Set interface provides methods for accessing the elements of a finite mathematical set. Sets do not allow duplicate elements.

-------------------------------------------------------------------------------------------------
18).Which java.util classes and interfaces support event handling?

The EventObject class and the EventListener interface support event processing.

-------------------------------------------------------------------------------------------------
19). What is the Dictionary class?

The Dictionary class provides the capability to store key-value pairs.

-----------------------------------------------------------------------------------------------------------

20) What's the main difference between a Vector and an ArrayList?

Java Vector class is internally synchronized and ArrayList is not.

-------------------------------------------------------------------------------------------------

21)What is HashMap and Map?

Map is Interface and Hashmap is class that implements that.

-------------------------------------------------------------------------------------------------
22)Difference between HashMap and HashTable?

The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls. (HashMap allows null values as key and value whereas Hashtable doesnt allow). HashMap does not guarantee that the order of the map will remain constant over time. HashMap is unsynchronized and Hashtable is synchronized.

=====================================================================

Some util package interview questions

JAVA UTIL PACKAGE(java.utl.*)

1). What is Collection Framework?

The Collection API is a set of classes and interfaces that support operation on collections of objects. These classes and interfaces are more flexible, more powerful, and more regular than the vectors, arrays, and hashtables if effectively replaces.

Example of classes: HashSet, HashMap, ArrayList, LinkedList, TreeSet and TreeMap.
Example of interfaces: Collection, Set, List and Map.

2). What is ResourceBundle?

This class aids the internationalization of large program with many locale-specific resources.

3). What is ListResourceBundle?

This class aids the internationalization of large program with many locale-specific resources.

4). What is PropertyResourceBundle?

This class aids the internationalization of large program with many locale-specific resources.

5). What is Property permission?

The PropertyPermission allows you to grant a read/write permission to a system property.

6). What are the ad hoc classes?

Dictionary,Vector,Stack and Properties are ad hoc classes that are used to store and manipulate group of object.

7). When is UnspportedOperationException thrown?

If the collection cannot be modified when UnsupportedOperationException is thrown.

8). When is ClassCastException thrown?

A ClassCastException is generated when one object is incompatible with another object that means when attempt is made to add an incompatible object to a collection.

9). What is list?

The list interface extends Collection and declares the behavior of a collection that stores a sequence of elements. Elements can be inserted and accessed by their position in the list, using zero-based index. A list may contain duplicate elements.

10). What is set?

The Set interface defines a set. It extends Collection and declares the behavior of a collection. It does not allowed duplicate elements. It does not define any additional methods of its own.

11). What is SortedSet?

The SortedSet interface extends Set and declates the behavior of set that sorted elements in ascending order.

12). What is ArrayList?

The ArrayList extends AbstractList and implements List interface. ArrayList support dynamic arrays that can grow as needed.

13). What is difference between Array and ArrayList?

In java, standard array is fixed length. After array is created, we cannot increase or decrease the size of array. So we must know in advance how many elements an array will hold. But sometime, we don’t know util run time how large of array we need. To handle this situation, the collection framework defines arrayList.

14). What is linkedlist?

The LinkedList class extends AbstractSequentialList and implements List interface. It support linked list data structure.

15). What is hashSet?

The HashSet class extends AbstractSet that implements Set interface. It creates collection that uses hash table for storage.

16). What is hashing?

The hashtable stores elements by using a mechanism called hashing.

17). What is hashCode?

In hashing, the informational content of key is used to determine a unique value, called hashCode.

18). What is LinkedHashSet?

The LinkedHashSet extends HashSet class. This class maintains a linked list of entries in the set,in order in which they were inserted.

19). What is treeset?

The TreeSet class implements Set interface. It uses a tree for storage. Object are stored in sorted, ascending order. Access and retrieval time are quite fast so we use TreeSet when we store large amount of sorted information.

20). What is iterator?

The iterator are used to cycle through the elements in a collection.

21). What is listIterator?

ListIterator allow bidiractional traversal of a list.

22). What is RandomAccessInterface?

23). What is map?

The map is object that store association between keys and values,or key/value pair. We give key,we will find value. Both key and value are objects. The key is unique but the values are duplicates.

24). What is map.Entry?

The Map.Entry describes an element in a map. This is inner class of Map.

25). What is SortedMap?

The SortedMap extends Map so that the keys are maintained in ascending order.

26). What is hashMap?

The HashMap class use a hash table to implement the Map interface. This allows the execution time of basic operation such as get() and put(),to remain constant even for large set.

27). What is TreeMap?

The TreeMap implements the Map interface by using a tree. A TreeMap provides way for storing key/value pair in sorted order. This allow rapid retrieval.

28). What is LinkedHashMap?

The LinkedHashMap extends HashMap. This maintains a linked list of the entries in the map,in the order in which they are inserted.

29). What is the use of Comparator?

The comparator defines how two objects are compare.

30). What are legacy classes?

Dictionaries, Hashtable, Properties, Stack, Vector are legacy classes.

31). What are legacy interface?

There is one legacy interface called Enumeration.

32). What is the use of Enumeration?

The Enumeration interface defines the methods by which we can enumerate that means obtain one element at a time in a collection of object.

33). What is the use of Vector?

Vector implements a dynamic array.

34). What is the difference between Vector and ArrayList?

Vector implements a dynamic array. It is similar to ArrayList,but with two differences. Vector is synchronized and it contains many legacy methods that are not part of the collection framework.

35). What is the use of stack?

Stack is subclass of Vector that implements a standard last-in, first-out stack.

36). What is Dictionary?

Dictionary operates like map. Given key, we retrieve value.

37). What is HashTable?

It is similar to HashMap but it is synchronized. Like HashMap, HashTable stores key/value pairs in a hash table.

38). What is difference between Hashmap and HashTable?

HashMap contain null values. Whereas HashTable does not allowed null values.

HashMap in not synchronized. Whereas HashTable is synchronized.

39). What is the use of Properties?

Properties is a subclass of HashTable. It is used to maintain lists of values in which the key is a String and the value is String.

40). What is the use of store() and load() in properties class?

Properties object can be easily stored to or loaded from disk with the store() and load() methods.


e

1).


2 comments:

Unknown said...

Hai friends,

It is very useful for java interview. Please post another topic interview questions.

Unknown said...

Hello guys,
Please send multi threading interview questions.