Menu
Topics Index
...
`

Collections BitSet


If you need explanation Read this topic

If you need Answer Take test on this topic

Collections BitSet
What will be the output of the following program?
import java.util.*;
public class BitsetTester {
    public static void main(String[] args) {
        BitSet b1 = new BitSet(10);
        b1.set(1);    b1.set(4);    b1.set(3);
        BitSet b2 = new BitSet(10);
        b2.set(4);    b2.set(5);    b2.set(3);
        System.out.print(b1 + ", ");
        System.out.println(b2);
        if (b1.equals(b2)) {    System.out.println("Equal");    }
        else {    System.out.println("Not Equal");    }
        BitSet clone = (BitSet) b1.clone();
        if (b1.equals(clone)) {    System.out.println("Equal");    }
        else {    System.out.println("Not Equal");    }
        b1.and(b2);
        System.out.println(b1);
    }
}

A. [4, 3, 1], [5, 4, 3]
Not Equal
Equal
[5, 4, 3]
B. {4, 3, 1}, {5, 4, 3}
Equal
Not Equal
{5, 4, 3}
C. [1, 3, 4], [3, 4, 5]
Not Equal
Equal
[1, 3, 4, 3, 4, 5]
D. {1, 3, 4}, {3, 4, 5}
Not Equal
Equal
{3, 4}
E. Some other output
F. Compilation Error or Runtime Error
Topic: Java BitSet

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

answer id D...void set(int index):Sets the bit specified by index.
the b1 would be [1,3,4] and b2 would be [3,4,5]..
boolean equals(Object bitSet):Returns true if the invoking bit set is equivalent to the one passed in bitSet. Otherwise, the method returns false.As b1 is not equal to b2,it returns "not equal"...
Clone duplicates the invoking BitSet object...thus the next equals method returns "Equal"...
AND method returns the common bits....i.e.,{3,4}..So finally the result is D...

Posted by Sneha Rapool    2014-07-11 09:01:44


Congratulations Sneha Rapool. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-07-14 11:58:31


© meritcampus 2019

All Rights Reserved.

Open In App