Share on Google+Share on Google+

SCJP Module-9 Question-18

Advertisement
The Sample program given below will test your understanding about the HashSet class of collection framework in Java.

Given below the sample code :

1.  import java.util.*;
2.  public class MyString {
3.  private String a;
4.  public MyString(String a) { this.a = a; }
5.  public static void main(String[] args) {
6.  HashSet<Object> hash = new HashSet<Object>();
7.  MyString ms1 = new MyString("Roseindia");
8.  MyString ms2 = new MyString("Roseindia");
9.  String a1 = new String("Roseindia");
10. String a2 = new String("Roseindia");
11. hash.add(ms1); hash.add(ms2); hash.add(a1); hash.add(a2);
12. System.out.println(hash.size()); } }

What will be the output of the above code ?

1. 0

2. 1

3. 2

4. 3

Answer

(4)

Advertisements

Posted on: July 17, 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.