Home Tutorial Java Scjp Part9 SCJP Module-9 Question-1

 
 

SCJP Module-9 Question-1
Posted on: July 16, 2010 at 12:00 AM
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'.

Related Tags for SCJP Module-9 Question-1:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.