What will be the output of the following Java code?

import java.util.*;
   class Bitset
   {
       public static void main(String args[])
       {
           BitSet obj1 = new BitSet(5);
           BitSet obj2 = new BitSet(10);
           for (int i = 0; i < 5; ++i)
               obj1.set(i);
           for (int i = 3; i < 13; ++i)
               obj2.set(i);
           obj1.and(obj2);
           System.out.print(obj1);
       }
   }

a) {0, 1}
b) {2, 4}
c) {3, 4}
d) {3, 4, 5}

1 thought on “What will be the output of the following Java code?”

  1. c
    Explanation: obj1.and(obj2) returns an BitSet object which contains elements common to both the object obj1 and obj2 and stores this BitSet in invoking object that is obj1. Hence obj1 contains 3 & 4.
    Output:
    $ javac Bitset.java
    $ java Bitset
    {3, 4}

Leave a Comment