Then you're right, there's an error in the practice exam. That method is fine for the cases where
o is a non-null
A object, but it will throw a NullPointerException if
o is null. The contract
java.lang.Object#equals(java.lang.Object) says:
- For any non-null reference value x, x.equals(null) should return false.
It will also throw a ClassCastException if
o is a different type, which also doesn't meet the statement "returns
true if this object is the same as the obj argument;
false otherwise".