🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Records and Sealed Classes: all 10 questions

Quiz: Records and Sealed Classes · Question 3 of 10

What does this print? (code)

What does this print?

import java.util.*;
record City(String name) { }
public class Main {
    public static void main(String[] args) {
        Set<City> set = new HashSet<>();
        set.add(new City("Pune"));
        set.add(new City("Pune"));
        System.out.println(set.size());
    }
}
  1. A.2
  2. B.1
  3. C.0
  4. D.Compile error
Answer & explanation

Correct answer: 1

Records generate equals and hashCode from their fields, so both City("Pune") objects are equal and the set keeps one.

Can you get the rest right without peeking?

Take the timed Quiz: Records and Sealed Classes quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →