Menu
Topics Index
...
`

BitSet2899


If you need explanation Read this topic

If you need Answer Take test on this topic

BitSet2899
What will be the output of the following program?
import java.util.*;
public class BitsAre {
    public static void main(String[] args) {
        BitSet bs = new BitSet();
        for (String s : new String[]{"LeaRn", "PracTice", "coMpetE"}) {
            for (int i = 0; i < s.length(); i++) {
                if (Character.isLowerCase(s.charAt(i)))
                    bs.set(i);
            }
            System.out.println(bs);
        }
    }
}

A. {1, 2, 4}
{1, 2, 3, 5, 6, 7}
{0, 1, 3, 4}
B. {1, 2, 4}
{1, 2, 3, 4, 5, 6, 7}
{0, 1, 2, 3, 4, 5, 6, 7}
C. {1, 2, 4}
{1, 2, 3, 5, 6, 7}
{0, 1, 3, 4, 6, 7}
D. Some other output
E. 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-B

Posted by Sivani Reddy Kakuru    2014-11-26 12:47:05


Ans is B..BitSet is an special type of array that can hold the Bit values and this can b increased as needed and index starts from the pstv numbers only...

Set() method which is used to set the Particular bit to 1...
Coming to the program here we create the array of Strings with 3 elements...

using for-each loop we are travelling every time we are checking if the char is lowercase than we are setting that index bit to 1..so here in the 1st string LeaRn..than 1,2,4 are lower case so it will set that locations to 1..

PracTice -- 1, 2, 3, 4, 5, 6, 7  are small letters so that indexes set to 1similar to otehr one also

Posted by Uday Kumar    2014-11-26 12:47:24


ans b.The BitSet class implements a set of bits. The set grows in size as needed. Each element of a BitSet has
a boolean value. When a BitSet object is created, all of the bits are set to false by default. The bits in a
BitSet are indexed by nonnegative integers, starting at 0. The size of a BitSet is the number of bits that
it currently contains. The BitSet class provides methods to set, clear, and retrieve the values of the
individual bits in a BitSet.  constructor creates a BitSet with a default size of 64 bits. All of the bits in the BitSet are
initially set to false.                                                                                            coming to this program string array initialized with 3 strings,                                         nested for loop concept used here.                                                                            s="LeaRn" inner for loop checks each char from string thn if condition chcks lowercase or not,if char is lowercase then thn bit value is set to true.                   set()-This method sets the bit at the given index to true. If bit is greater than or equal to the number of
bits in the BitSet, the size of the BitSet is increased so that it contains bit values.here 1,2,4th bits set to true.    sop prints 1,2,4                                                                           s="PracTice" here 1,2,3,5,6,7 bits are set to true.so bitset ve following true bits 1,2,3,4,5,6,7 so nw sop prints 1,2,3,4,5,6,7.                                                            s="coMpetE" from this string 0,1,3,4,5 bits are true.bitset ve following true bits 0,1,2,3,4,5,6,7 thn sop get printd

Posted by Maheshwari Natarajan    2014-11-26 15:52:38


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Maheshwari Natarajan,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Akla Ame. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2014-11-27 08:22:50


© meritcampus 2019

All Rights Reserved.

Open In App