wrapper classes in Java

wrapper classes in Java

1. Why do we need wrapper classes ?

A.1. They convert primitive data types into objects and this is needed on Internet to mommunicate between two applications.
2. The classes in java.util package handle only objects and hence wrapper classes help in this case also.

2. Which of the wrapper classes contains only one constructor ? (or) Which of the wrapper classes does not contain a constructor with String as parameter ?

A. Character.

3. What is unboxing ?

A. Converting an object into its corresponding primitive datatype is called unboxing.

4. What happens if a string like “ Hello” is passed to parseInt ( ) method ?

A. Ideally a string with an integer value should be passed to parseInt ( ) method. So, on parsing
“Hello”, an exception called “NumberFormatException’ occurs since the parseInt( ) method cannot
convert the given string “Hello” into an integer value.

5.What is a collection framework ?

A. A collection framework is a class library to handle groups of objects. Collection framework is
implemented in java.util.package.

6. Does a collection object store copies of other objects or their references ?

A. A Collection object stores references of other objects.

7. Can you store a primitive data type into a collection ?

A. No, Collections store only objects.

8. What is the difference between Iterator and ListIterator ?

A. Both are useful to retreive elements from a collection. Iterator can retrieve the elements only in
forward direction. But Listener can retrieve the elements in forward and backward direction also. So
ListIterator is preferred to Iterator.

9. What is the difference between Iterator and Enumeration ?

A. Both are useful to retreive elements from a collection. Iterator has methods whose names are
easy to follow and Enumeration methods are difficult to remember. Also Iterator has an option to
remove elements from the collection which is not available in Enumeration. So, Iterator is preferred to
Enumeration.

10. What is the difference between a Stack and LinkedList ?

A. 1. A Stack is generally used for the purpose of evaluation of expression. A LinkedList is used to
store and retrieve data.
www.students3k.com
2. Insertion and deletion of elements only from the top of the Stack is possible. Insertion and
deletion of elements from any where is possible in case of a LinkedList.

11. What is the difference between ArrayList and Vector ?

A.ArrayList Vector
ArrayList object is not synchronized by Vector object is synchronized by default. default
Incase of a single thread, using ArrayList is faster than the Vector.
In case of multiple threads, using Vector is advisable. With a single thread, Vector
becomes slow. ArrayList increases its size every time by 50
percent (half). Vector increases its size every time by
doubling it.

12. Can you synchronize the ArrayList object ?

A. Yes, we can use synchronizedList( ) method to synchronize the ArrayList, as:
Collections.synchronizedList(new ArrayList( ));

13. What is the load factor for a HashMap or Hashtable ?
A. 0.75.

14. What is the difference between HashMap and Hashtable ?

A.HashMap Hashtable
HashMap object is not synchronized by default. Hashtable object is synchronized by default.
In case of a single thread, using HashMap is faster than the Hashtable.
In case of multiple threads, using Hashtable is advisable, with a single thread, Hashtable becomes slow.
HashMap allows null keys and null values to be stored.
Hashtable does not allow null keys or values.
Iterator in the HashMap is fail-fast. This means Iterator will produce exeception if concurrent updates are made to the HashMap.
Enumeration for the Hashtable is not fail-fast. This means even if concurrent updations are done to Hashtable, there will not be any incorrect results
produced by the Enumeration.

15. Can you make HashMap synchronized ?
A. Yes, we can make HashMap object synchronized using synchronizedMap( ) method as shown here:
Collections.synchronizedMap(new HashMap( ));

16. What is the difference between a Set and a List ?

A.Set List
A set represents a collection of elements. Order of the elements may change in the set.
A List represents ordered collection of elements.List preserves the order of elements in which they are entered.
Set will not allow duplicate values to be stored. List will allow duplicate values.
Accessing elements by their index (position number) is not possible in case of sets.
Accessing elements by index is possible in lists.
Sets will not allow null elements. Lists allow null elements to be stored.

0 comments:

Post a Comment