ควิซ Java: equals() & hashCode()

This Java quiz goes deep into equals() and hashCode(), a favorite interview topic. You will predict how objects behave in a HashSet, catch an equals that overloads instead of overrides, and meet the Integer cache and records.

ยาก 10 ข้อ ~8 นาที ผ่านที่ 80% Java 17

เลือกโหมด

กำลังเล่นแบบผู้เยี่ยมชม: เข้าสู่ระบบเพื่อเก็บ XP และ streak

ตัวอย่างคำถาม

ข้อ 1 จาก 10

Which hashCode() for a class with int fields x and y follows the contract but makes a HashMap with many keys very slow?

ดูเฉลย

C. return 42; A constant hash is legal, since equal objects still get equal hash codes, but every key lands in the same bucket and lookups degrade to scanning. Good hash codes spread different objects out.

ควิซนี้มีเนื้อหาอะไรบ้าง

  • Reference equality vs equals()
  • The equals/hashCode contract
  • Why HashSet and HashMap need both methods
  • Mutable keys and overloaded equals
  • The Integer cache and records

คำถามที่พบบ่อย

ต้องมีบัญชีไหม?

ไม่ต้อง ใครก็เล่นได้ เข้าสู่ระบบเพื่อเก็บ XP, streak, เหรียญตรา และประวัติ

ทำซ้ำได้ไหม?

ได้ ระบบเก็บคะแนนที่ดีที่สุดไว้ และสลับคำถามกับตัวเลือกใหม่ทุกครั้ง