Java API Interview Questions

This page contains the most common and practicle Java interview questinos with answers. For more questions on various topics of core java navigate thorough the links privided in below table.

Overview : Most Imp Questions Java Basic Interview Questions
OOPS Concepts Interview Questions Exception Handling Questions
MultiThreading & Concurrency API Java API : String, File I/O
Collection Framework Questions Advanced Questions

Object class is parent class of all other class in Java. Its methods are:
equals(), hasCode(), wait(), notify().
If two objects are equal (equals return true for them) there hashCode must be same.
However, if two methods have same hashCode, they might or might not be equal.
The Hashing mechanism work on 2 methods: hashCode and equals.
The main difference between String and StringBuffer is that String objects are immutable while StringBuffer is mutable. So that means you can change value of StringBuffer objects, which is not case with String. So when your string need to be modified frequently you should use StringBuffer. Also, all public methods of StringBuffer are synchronized that makes it slow.
StringBuffer is an ideal class to use instead of String when your strings has to go through lot of modification. But problem with StringBuffer is that its synchronized making it slow. The same problem is overcome by StringBuilder. So StringBuilder and StringBuffer are similar except that StringBuilder is not synchronized.
Comparablle and Comparator both are interfaces in java.util package.
Comparable Comparator
Any class which implements Comparable becomes Comparable. A class that implements Comparator is a third party class which is used to compare objects of another class.
It has one method : int compareTo(T obj). It has method : int compare(T obj1, T obje2)
If you want to compare objects of a class using Comparable, the class must implement Comparable interface. By using Comparator, you compare can compare objects of class that don't implement Comparable/Comparator.
There can be only one algorithm by which you can compare the objects. You can create N types of Comparators to compare the objects in N ways.
Most of collection classes and arrays are comparable. No class in Java API implement it. You create your own comparator.
If your class is comparable, a list (or array) of its objects can be sorted automatically by Collections.sort() or Arrays.sort() method If your class is not comparable, you can still sort its list using sort methods, but you need to specify the comparator as extra parameter to sort method.
Objects that implement this interface can be used as keys in a sorted map or as elements in a sorted set, without the need to specify a comparator. Objects that don't implement Comparable, can still be used as keys in sorted map but they must either override hashCode and equals methods or specify a comparator.

Note: 1. The two objects that you compare with compare or compareTo method must be mutually comparable otherwise the methods may throw ClassCastException.
2. It is strongly recommended that the ordering done by compareTo (or compare) methods should be consistent with equals() method.
FileInputStream, FileOutputStream.
FileReader, FileWriter
BufferedReader, BufferedWriter.

<< Previous Page : 1 2 3 4 5 6 7 8 Next >>