This set of Java Multiple Choice Questions & Answers (MCQs) on “Java.util – BitSet & Date class”.
1. Which of these class object has an architecture similar to that of array? Answer: a 2. Which of these method is used to make a bit zero specified by the index? Answer: d 3. Which of these method is used to calculate number of bits required to hold the BitSet object? Answer: b 4. Which of these is a method of class Date which is used to search whether object contains a date before the specified date? Answer: c 5. Which of these methods is used to retrieve elements in BitSet object at specific location? Answer: a 6. What will be the output of the following Java code? a) {0, 1, 3, 4} 7. What will be the output of the following Java code? a) 4 64 8. What will be the output of the following Java code? a) 2 9. What will be the output of the following Java code? a) Prints Present Date 10. What will be the output of the following Java code? a) {0, 1}
a) Bitset
b) Map
c) Hashtable
d) All of the mentioned
Clarification: Bitset class creates a special type of array that holds bit values. This array can increase in size as needed.
a) put()
b) set()
c) remove()
d) clear()
Clarification: None.
a) size()
b) length()
c) indexes()
d) numberofBits()
Clarification: None.
a) after()
b) contains()
c) before()
d) compareTo()
Clarification: before() returns true if the invoking Date object contains a date that is earlier than one specified by date, otherwise it returns false.
a) get()
b) Elementat()
c) ElementAt()
d) getProperty()
Clarification: None.
import java.util.*;
class Bitset
{
public static void main(String args[])
{
BitSet obj = new BitSet(5);
for (int i = 0; i < 5; ++i)
obj.set(i);
obj.clear(2);
System.out.print(obj);
}
}
b) {0, 1, 2, 4}
c) {0, 1, 2, 3, 4}
d) {0, 0, 0, 3, 4}
Clarification: None.
Output:
$ javac Bitset.java
$ java Bitset
{0, 1, 3, 4}
import java.util.*;
class Bitset
{
public static void main(String args[])
{
BitSet obj = new BitSet(5);
for (int i = 0; i < 5; ++i)
obj.set(i);
obj.clear(2);
System.out.print(obj.length() + " " + obj.size());
}
}
b) 5 64
c) 5 128
d) 4 128
Clarification: obj.length() returns the length allotted to object obj at time of initialization and obj.size() returns the size of current object obj, each BitSet element is given 16 bits therefore the size is 4 * 16 = 64, whereas length is still 5.
Output:
$ javac Bitset.java
$ java Bitset
5 64
import java.util.*;
class Bitset
{
public static void main(String args[])
{
BitSet obj = new BitSet(5);
for (int i = 0; i < 5; ++i)
obj.set(i);
System.out.print(obj.get(3));
}
}
b) 3
c) 4
d) 5
Clarification: None.
Output:
$ javac Bitset.java
$ java Bitset
2
import java.util.*;
class date
{
public static void main(String args[])
{
Date obj = new Date();
System.out.print(obj);
}
}
b) Runtime Error
c) Any Garbage Value
d) Prints Present Time & Date
Clarification: None.
Output:
$ javac date.java
$ java date
Tue Jun 11 11:29:57 PDT 2013
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);
}
}
b) {2, 4}
c) {3, 4}
d) {3, 4, 5}
Clarification: 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}