What is the result?

Given:

3. import java.util.*;
4. public class Mapit {
5. public static void main(String[] args) {
6. Set<Integer> set = new HashSet<Integer>();
7. Integer i1 = 45;
8. Integer i2 = 46;
9. set.add(i1);
10. set.add(i1);
11. set.add(i2); System.out.print(set.size() + ” “);
12. set.remove(i1); System.out.print(set.size() + ” “);
13. i2 = 47;
14. set.remove(i2); System.out.print(set.size() + ” “);
15. }
16. }

What is the result?

Given:

3. import java.util.*;
4. public class Mapit {
5. public static void main(String[] args) {
6. Set<Integer> set = new HashSet<Integer>();
7. Integer i1 = 45;
8. Integer i2 = 46;
9. set.add(i1);
10. set.add(i1);
11. set.add(i2); System.out.print(set.size() + ” “);
12. set.remove(i1); System.out.print(set.size() + ” “);
13. i2 = 47;
14. set.remove(i2); System.out.print(set.size() + ” “);
15. }
16. }

What is the result?

A.
2 1 0

B.
2 1 1

C.
3 2 1

D.
3 2 2

E.
Compilation fails.

F.
An exception is thrown at runtime.



Leave a Reply 0

Your email address will not be published. Required fields are marked *