Share on Google+Share on Google+

SCJP Module-9 Question-1

Ads
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'.

Advertisements

Posted on: July 16, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.