Java MCQ – Collection Framework

This section of our 1000+ Java MCQs focuses on collection framework of Java Programming Language.

1. Which of these packages contain all the collection classes?
a) java.lang
b) java.util
c) java.net
d) java.awt
View Answer

Answer: b
Explanation: None.

2. Which of these classes is not part of Java’s collection framework?
a) Maps
b) Array
c) Stack
d) Queue
View Answer

Answer: a
Explanation: Maps is not a part of collection framework.

3. Which of this interface is not a part of Java’s collection framework?
a) List
b) Set
c) SortedMap
d) SortedList
View Answer

Answer: d
Explanation: SortedList is not a part of collection framework.
advertisement
advertisement

4. Which of these methods deletes all the elements from invoking collection?
a) clear()
b) reset()
c) delete()
d) refresh()
View Answer

Answer: a
Explanation: clear() method removes all the elements from invoking collection.

5. What is Collection in Java?
a) A group of objects
b) A group of classes
c) A group of interfaces
d) None of the mentioned
View Answer

Answer: a
Explanation: A collection is a group of objects, it is similar to String Template Library (STL) of C++ programming language.
Note: Join free Sanfoundry classes at Telegram or Youtube

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

  1.     import java.util.*;
  2.     class Array 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             int array[] = new int [5];
  7.             for (int i = 5; i > 0; i--)
  8.                 array[5-i] = i;
  9.             Arrays.fill(array, 1, 4, 8);
  10.             for (int i = 0; i < 5 ; i++)
  11.                 System.out.print(array[i]);
  12.         }
  13.     }

a) 12885
b) 12845
c) 58881
d) 54881
View Answer

Answer: c
Explanation: array was containing 5,4,3,2,1 but when method Arrays.fill(array, 1, 4, 8) is called it fills the index location starting with 1 to 4 by value 8 hence array becomes 5,8,8,8,1.
Output:

advertisement
$ javac Array.java
$ java Array
58881

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

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);
  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:

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

Sanfoundry Global Education & Learning Series – Java Programming Language.

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.