Scrivio Interview library

Object methods and contracts

22 questions in 6 subtopics.

Equals and hashCode contract

ConceptFoundations6 min read
State the equals contract and the hashCode contract. What breaks in a HashMap if a class overrides equals but not hashCode?
CodingFoundations7 min read
Write a correct equals and hashCode for final class Money { private final long cents; private final String currency; }.
DebuggingFoundations6 min read
A HashMap<String[], Integer> never finds keys that look identical. Why?
ConceptIntermediate7 min read
Why is it hard to keep equals symmetric and transitive when a subclass adds a value field, such as ColorPoint extends Point? What are the options?
DebuggingIntermediate7 min read
Objects are added to a HashSet, then one of their fields used in hashCode is changed. Later contains returns false and remove does nothing, but iteration shows the object. Explain.
DesignAdvanced7 min read
Your organization wants a standard for equals and hashCode across domain models, JPA entities, DTOs and records. What would the standard say?
LeadershipPrincipal6 min read
Bugs from broken equals and hashCode have appeared in your organization for years: mutable keys, missing hashCode, entity equality mistakes. What would you standardise or forbid, and how do you know it worked?

ToString

ConceptFoundations6 min read
What does Object.toString return by default, and what makes a good toString override? What should never be in it?
DebuggingIntermediate6 min read
Logging a JPA entity with log.info("{}", order) causes extra SQL queries and once a StackOverflowError. Why?
DesignAdvanced7 min read
A security audit finds personal data and tokens in logs, mostly coming from toString output of DTOs, records and exceptions. What standards and library support do you introduce across services?

Comparable and Comparator

CodingFoundations5 min read
Write a Comparator that sorts employees by department ascending, then salary descending, then name, with nulls in department sorted last.
DebuggingFoundations6 min read
A comparator is written as (a, b) -> a.getId() - b.getId() for int IDs. Sorting occasionally gives wrong order. Why?
ConceptIntermediate6 min read
What does it mean for compareTo to be consistent with equals, and what goes wrong in a TreeSet or TreeMap when it is not? Give a JDK example.
DesignAdvanced6 min read
You are designing sort and ranking rules for a multi-tenant search API with locale-specific names. What do you need beyond Comparator.comparing(Item::name)?
DesignPrincipal7 min read
Sort orders are embedded across your systems: in Comparable implementations, database ORDER BY clauses, search indexes and pagination cursors, and they disagree. How do you set a long-term approach to ordering?

Clone and copy constructors

ConceptFoundations6 min read
Why is Cloneable considered broken, and what do you use instead?
CodingIntermediate7 min read
Write a deep copy constructor for this class and explain which fields need copying:
class Team {
private final String name;
private final List<Member> members;
private final Map<String, Role> roles;
}

Finalize deprecation and Cleaner

ConceptIntermediate7 min read
Why was finalize() deprecated (and deprecated for removal since JDK 18), and what are Cleaner and try-with-resources used for instead?
ScenarioAdvanced7 min read
Before a JDK upgrade you want to know whether any of your services or their dependencies still rely on finalization. How do you find out and handle it?
ConceptPrincipal6 min read
Finalization was part of Java from 1.0 and took over twenty years to be deprecated for removal. What does that history say about designing cleanup and lifecycle APIs, and what would you require of your own libraries?

Identity vs equality

ConceptFoundations7 min read
Explain the difference between == and equals for objects. What does System.identityHashCode return, and when would you use IdentityHashMap?
ConceptPrincipal7 min read
Project Valhalla proposes value classes whose instances have no identity. Explain why Java originally gave every object identity, what that has cost, and how you would prepare a large codebase.