Java Questions & Answers – Java.util – BitSet & Date class

This set of Java Multiple Choice Questions & Answers (MCQs) focuses on “Java.util – BitSet & Date class”.

1. Which of these class object has an architecture similar to that of array?
a) Bitset
b) Map
c) Hashtable
d) All of the mentioned
View Answer

Answer: a
Explanation: Bitset class creates a special type of array that holds bit values. This array can increase in size as needed.

2. Which of these method is used to make a bit zero specified by the index?
a) put()
b) set()
c) remove()
d) clear()
View Answer

Answer: d
Explanation: None.

3. Which of these method is used to calculate number of bits required to hold the BitSet object?
a) size()
b) length()
c) indexes()
d) numberofBits()
View Answer

Answer: b
Explanation: None.
advertisement
advertisement

4. Which of these is a method of class Date which is used to search whether object contains a date before the specified date?
a) after()
b) contains()
c) before()
d) compareTo()
View Answer

Answer: c
Explanation: before() returns true if the invoking Date object contains a date that is earlier than one specified by date, otherwise it returns false.

5. Which of these methods is used to retrieve elements in BitSet object at specific location?
a) get()
b) Elementat()
c) ElementAt()
d) getProperty()
View Answer

Answer: a
Explanation: None.
Note: Join free Sanfoundry classes at Telegram or Youtube

6. What will be the output of the following Java code?

  1.     import java.util.*;
  2.     class Bitset
  3.     {
  4.         public static void main(String args[])
  5.         {
  6.             BitSet obj = new BitSet(5);
  7.             for (int i = 0; i < 5; ++i)
  8.                 obj.set(i);
  9.             obj.clear(2);
  10.             System.out.print(obj);
  11.         }
  12.     }

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

Answer: a
Explanation: None.
Output:

advertisement
$ javac Bitset.java
$ java Bitset
{0, 1, 3, 4}

7. What will be the output of the following Java code?

advertisement
  1.     import java.util.*;
  2.     class Bitset 
  3.     {
  4.         public static void main(String args[])
  5.         {
  6.             BitSet obj = new BitSet(5);
  7.             for (int i = 0; i < 5; ++i)
  8.                 obj.set(i);
  9.             obj.clear(2);
  10.             System.out.print(obj.length() + " " + obj.size());
  11.         }
  12.     }

a) 4 64
b) 5 64
c) 5 128
d) 4 128
View Answer

Answer: b
Explanation: 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

8. What will be the output of the following Java code?

  1.     import java.util.*;
  2.     class Bitset
  3.     {
  4.         public static void main(String args[])
  5.         {
  6.             BitSet obj = new BitSet(5);
  7.             for (int i = 0; i < 5; ++i)
  8.                 obj.set(i);
  9.             System.out.print(obj.get(3));
  10.         }
  11.     }

a) 2
b) 3
c) 4
d) 5
View Answer

Answer: a
Explanation: None.
Output:

$ javac Bitset.java
$ java Bitset
2

9. What will be the output of the following Java code?

  1.     import java.util.*;
  2.     class date
  3.     {
  4.         public static void main(String args[])
  5.         {
  6.             Date obj = new Date();
  7.             System.out.print(obj);
  8.         }
  9.     }

a) Prints Present Date
b) Runtime Error
c) Any Garbage Value
d) Prints Present Time & Date
View Answer

Answer: d
Explanation: None.
Output:

$ javac date.java
$ java date
Tue Jun 11 11:29:57 PDT 2013

10. What will be the output of the following Java code?

  1.     import java.util.*;
  2.     class Bitset
  3.     {
  4.         public static void main(String args[])
  5.         {
  6.             BitSet obj1 = new BitSet(5);
  7.             BitSet obj2 = new BitSet(10);
  8.             for (int i = 0; i < 5; ++i)
  9.                 obj1.set(i);
  10.             for (int i = 3; i < 13; ++i)
  11.                 obj2.set(i);
  12.             obj1.and(obj2);
  13.             System.out.print(obj1);
  14.         }
  15.     }

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

Answer: 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}

Sanfoundry Global Education & Learning Series – Java Programming Language.

To practice all areas of Java language, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.