What are good interview questions for Java developers?

Interviewer: If compareTo() method returns zero this means that the objects are always equal?
Me:Yes.
But I was wrong at that point its not necessary the example displaying the failure of this is  java.math.BigDecimal class, whose equals method will return true is the value is equal in both value and scale for ex:- 6.0 and 6.00 will not be equal, but compareto() will return 0 if both objects are compared.
On doing some R&D I found that:
It is strongly recommended, but not strictly required that(x.compareTo(y)==0) == (x.equals(y)). Generally speaking, any class that implements the Comparable interface and violates this condition should clearly indicate this fact. The recommended language is "Note: this class has a natural ordering that is inconsistent with equals." 

This question can also be asked in an other way as:
Can we store BigDecimal Class in TreeSet?
Anser: NO because of the above reason.

I would like to add few more common questions from different sources so that it may be helpful for someone and myself also when some quick revision is required.
While browsing whole day through google I found some of the websites which you may find interesting and help you to perform good in interview's:

Top 20 Core Java Interview Questions and Answers asked on Investment Banks
Java Source Code Examples
Java Programming Exercises
Top 10 Algorithms for Coding Interview
Hunting Memory Leaks in Java


Comments