A parcel scanner compares an expected label with a scanned label. Either reference can be null. A null-safe comparison handles missing references without throwing an exception.
The programmer uses Objects.equals from java.util.Objects to compare the labels safely while preserving String value equality when both references contain strings.
import java.util.Objects; static boolean sameLabel(String expected, String actual) { return Objects.equals(expected, actual); }
Complete the results: sameLabel(null, null) returns , while sameLabel(null, "BX-42") returns .
Unlock full course notes by signing up
Check equality and hash-code compatibility
A Java program compares shipment identifiers using a class that obeys the standard equals and hashCode contracts. A hash code is the integer returned by an object's hashCode() method.
Each row describes an independent proposed result for two non-null objects, a and b. No relevant object state changes during a comparison. Enter permitted if the combination can satisfy the contracts, or forbidden if it violates them. Judge contractual possibility rather than the behavior of any particular implementation.
Test a proposed equality rule
A Java measurement class proposes that two instances are equal whenever their integer readings differ by at most 2 units. Three separate instances have readings 50, 52, and 54 units. No reading changes during comparison. Which statements are correct? Select all that apply.
Trace an equality method with a different signature
A ticketing program uses the Java class below. All ticket codes in this program are non-null strings.
The following statements run inside a method:
Among the six outer equality calls passed to println, how many select the equals(Ticket) method declared in Ticket, and how many evaluate to true? Exclude the internal code.equals(other.code) calls. Give both counts as exact integers.