Java MCQs 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
Answer: b
2. Which of these classes is not part of Java’s collection framework?
a) Maps
b) Array
c) Stack
d) Queue
Answer: a
Clarification: 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
Answer: d
Clarification: SortedList is not a part of collection framework.
4. Which of these methods deletes all the elements from invoking collection?
a) clear()
b) reset()
c) delete()
d) refresh()
Answer: a
Clarification: 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
Answer: a
Clarification: A collection is a group of objects, it is similar to String Template Library (STL) of C++ programming language.
6. What will be the output of the following Java program?
-
import java.util.*;
-
class Array
-
{
-
public static void main(String args[])
-
{
-
int array[] = new int [5];
-
for (int i = 5; i > 0; i--)
-
array[5-i] = i;
-
Arrays.fill(array, 1, 4, 8);
-
for (int i = 0; i < 5 ; i++)
-
System.out.print(array[i]);
-
}
-
}
a) 12885
b) 12845
c) 58881
d) 54881
Answer: c
Clarification: 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:
$ javac Array.java $ java Array 58881
7. What will be the output of the following Java program?
-
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);
-
}
-
}
a) {0, 1, 3, 4}
b) {0, 1, 2, 4}
c) {0, 1, 2, 3, 4}
d) {0, 0, 0, 3, 4}
Answer: a
Clarification: None.
Output:
$ javac Bitset.java $ java Bitset {0, 1, 3, 4}