Test 19 - Free Java SE 25 certification Mock test - Exam Code 1Z0-831-Oracle certified Java SE 25 Professional
Choosing the right collection is a daily Java decision and this free mock test for the Oracle 1Z0-831 exam is designed to sharpen that decision. The questions cover the Set, Map and Deque implementations and how they order their elements, how they deal with duplicates and how they treat null. You'll be able to work out what the result will be rather than just guessing.
You start with Sets. HashSet provides no ordering guarantee. LinkedHashSet maintains the order in which elements were inserted. TreeSet will keep the elements in sorted order (using natural ordering or a supplied Comparator). Items ask what a set will print out after a number of adds and what happens when you try to add an element that the TreeSet can't compare (which will result in a ClassCastException). You'll also see how a set determines that two objects are duplicates (in a hash based set this is via equals and hashCode, in a tree based set it's via compareTo) and that's a very important point of difference when the two disagree.
Maps get the biggest slice. You'll be using put, get, getOrDefault, putIfAbsent, computeIfAbsent, computeIfPresent, compute, merge, replaceAll and remove and you'll be asked what each one returns. The return value of put (the previous mapping) is a very common question. You'll be looking at HashMap, LinkedHashMap and TreeMap, the rules around null keys and null values and how you iterate over the keySet, values and entrySet. Immutable maps created via Map.of and the order in which they iterate are also covered. Be very wary of mutable keys in the hash based collections. If you alter a map key after it's been inserted then the entry becomes effectively unreachable and several of the questions in this test are based on that very real failure mode in short practical code.
Deque and Queue questions are all about the double ended nature of the data structure. ArrayDeque can be used as a stack (using push, pop and peek) or as a queue (using offer, poll and peek). You'll be expected to know which operations throw an exception when the container is empty (remove and element) and which return null or false (poll, peek and offer). A fairly common puzzle is to mix stack and queue operations on the same Deque, so you'll need to carefully trace the contents after each operation. PriorityQueue ordering is also tested and you'll need to know why simply printing it out won't give you the sorted order.
Every explanation will list the contents of the collection after each step. Take the test, tick the structures that you found confusing and then write a few very short programs to verify their behaviour. Once you're familiar with those rules then a whole set of collections questions become very much predictable points.
