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.


Java
final class Ticket {
    private final String code;

    Ticket(String code) {
        this.code = code;
    }

    public boolean equals(Ticket other) {
        return other != null && code.equals(other.code);
    }
}


The following statements run inside a method:


Java
Ticket first = new Ticket("R7");
Ticket second = new Ticket("R7");
Ticket alias = first;
Object stored = second;

System.out.println(first.equals(second));
System.out.println(first.equals(stored));
System.out.println(stored.equals(first));
System.out.println(first.equals(alias));
System.out.println(((Ticket) stored).equals(first));
System.out.println(((Object) first).equals(alias));


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.

Your answers

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Object Equality Questions: