Multiple choice technology programming languages

Program output : package com.collection.examples; import java.util.HashSet; public class HashSetExample { public static void main(String[] args) { HashSet s = new HashSet();//1 for(short i = 0; i<100;i++){ //2 s.add(i); //3 s.remove(i-1); //4 } System.out.println(s.size()); //5 } }

  1. NumberFormat Exception

  2. 100

  3. Compilation fails at line 4

  4. 99

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

The HashSet contains Short objects. In line 3, s.add(i) adds short values 0-99 (autoboxed to Short). In line 4, s.remove(i-1) fails because i-1 is an int expression, and remove(int) treats it as an index (which doesn't apply to Set) or fails to find a matching object. The remove(Object) method would be needed, but the int type prevents that. So all 100 Short objects remain, and size() returns 100.