Machine-translated by Google. Questions are authored in English.
Top

Discussion

Which line rests on a mistaken assumption?

public class Sample75
{
    public static void main(String[] args)
    {
        Object o = new Object();
        assert o.hashCode() == o.hashCode();
        assert new Object().hashCode() == new Object().hashCode();
        System.out.println("stable");
    }
}

  • A.System.out.println("stable");
  • B.Every line above is correct as written.
  • C.assert new Object().hashCode() == new Object().hashCode();
  • D.assert o.hashCode() == o.hashCode();

Answer: C

Asking the same object twice must give the same hash code, so the first assertion is sound. Two distinct objects have no such guarantee, and the default hash code is derived per instance, so the second is almost always false. Equal hash codes are required only for equal objects.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.