Skip to content
Sharp

Collections · interview question

Given a HashSet<Integer> with elements {1, 2, 3}, what will be the result of calling contains(2)?

Multiple choiceMediumchapter-4-curatedpremiumcollectionsset-interface-and-hashset
Choose one answer
Answer and explanation

Correct answer

A. True

Why

The contains() method will return true if the element 2 is present in the HashSet. HashSet is optimized to quickly check membership using the element's hash code.

Test yourself on Collections

A free test with instant, explained results. No account needed.

Start free test
All Collections questions →