SCJP Module-9 Question-18


 

SCJP Module-9 Question-18

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

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)

Ads