Java Interview Questions
1 .
What is difference between throws and throw?
Throws clause is used when the programmer does not want to handle the exception and throw it out of a method. throw clause is used when the programmer wants to throw an exception explicitly and wants to handle it using catch block. Hence, throw and throws are contradictory.
2 .
Why do we need wrapper classes?
1. They convert primitive datatypes to objects and this id needed on internet to communicate between two applications.
2. The classes in java.util package handle only objects and hence wrapper classes help in this case also.
3 .
Which of the wrapper class contains only one constructor?
Character.
4 .
What is boxing?
Converting a primitive datatype into an object is called ‘boxing’.
5 .
What is unboxing?
Converting a object into its primitive datatype is called ‘unboxing’.
6 .
What happens if a string “Hello” is passed to parseInt() method?
Ideally, a string with an integer value should be passed to parseInt() method. So, on passing “Hello” an exception called “NumberFormatException” occurs since the parseInt() method cannot convert the given string “Hello” into an integer value.
7 .
Does a collection object store copies of other objects or their references?
A collection object stores references of other objects.
8 .
Can you store primitive data type into a collection?
No, Collections store only objects.
9 .
What is the difference between Iterator and ListIterator?
Both are useful to retrieve elements from a collection. Iterator can retrieve the elements only in forward direction. But ListIterator can retrieve the elements in  forward and backward direction also. So the ListIterator is preferred to Iterator.
10 .
What is the difference between Iterator and Enumeration?
Both are useful to retrieve 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