banner Test 13

Test 13 - Free Java SE 25 certification Mock test - Exam Code 1Z0-831-Oracle certified Java SE 25 Professional

Every Java class inherits from Object and the 1Z0-831 exam is going to expect you to get the method overrides correct. This free Java SE 25 mock test is all about equals, hashCode, toString and the contracts that bind them. The questions are all designed to highlight the problems that can be caused by a poorly implemented version.

You start with equals. The contract is that it must be reflexive, symmetric, transitive and consistent and it returns false for null. The problems that the examples show are cases where an equals method has been overridden which accidentally overloads equals to take a parameter of the wrong type, hence the collection will never use that method. You'll see why the @Override annotation picks up that mistake immediately and you'll compare the use of instanceof with the use of getClass, particularly in class hierarchies where symmetry can be broken.

The hashCode contract comes next. Equal objects must have equal hash codes, unequal objects can still hash to the same value (i.e. they can collide). You'll look at classes that have overridden equals but not hashCode and then go on to predict the rather surprising behaviour you'll see in a HashSet or HashMap when you have duplicates (i.e. when you try to insert an object which is already present). You'll also have objects which use mutable fields in their hash calculation and so you'll be looking at what happens when that field changes after the object has been inserted. The various Objects utility methods (Objects.equals, Objects.hash and Objects.requireNonNull) will all be in play so you'll need to know how they all deal with null.

toString gets a look in as well. You'll be expected to know the default output (the class name plus a hex hash) and to be able to spot the case where string concatenation or a println will automatically invoke the overridden version. Records provide you with these three methods out of the box and there are a couple of questions which compare a manually written class with its record counterpart.

Other Object topics are clone and its protected access, getClass versus a class literal and why finalize is deprecated and really best avoided. You'll also be looking at how the various collection methods (ArrayList.contains, remove and indexOf) all rely on equals and how the sorted collections rely on compareTo or a comparator.

The explanations show each contract with a miniature counterexample. There's a cluster of items which mixes these contracts up with collections where one bad method will corrupt a HashMap. You have to work out whether the bug is in equals, in hashCode or in the mutability of the key and explain the symptom you see.

Finish the test, then go ahead and write your own equals and hashCode pair from memory to check that you actually understand it. A very short session of that will make these questions look absolutely trivial on the exam.

Welcome to your Free Java SE 25 – Test 13