SCJP Module-9 Question-1


 

SCJP Module-9 Question-1

The sample program given below your knowledge of HashSet class of collection framework.

The sample program given below your knowledge of HashSet class of collection framework.

Given below the sample code :

import java.util.HashSet;
public class CollectionExample {
public static void main(String ... args) {
HashSet<String> hs = new HashSet<String>();
String str1 = new String("Buzz");
String str2 = new String("Buzz");
hs.add(str1);
hs.add(str2);
System.out.println(hs);
}
}

What will be the output of the following ?

1. Compilation error

2. [Buzz,Buzz]

3. [Buzz]

4. Buzz

Answer

(3)

Explanation

The 'HashSet' doesn't allow to store duplicate collections. That's why 'Buzz' is stored once in HashSet 'hs'.

Ads