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 stores Short objects from 0 to 99. In each iteration, we add the current Short value, then attempt to remove i-1. However, i-1 is an int (arithmetic promotion), so remove() looks for an Integer object, not a Short. Since the HashSet contains only Shorts, the remove() calls never find matches. Therefore all 100 elements remain in the set.