Java Questions & Answers – Collection Framework Overview

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: d
Explanation: Queue is not a part of collection framework.

3. Which of these 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.

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.

6. What is the output of this program?

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

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:
$ javac Array.java
$ java Array
58881

7. What is the output of this program?

  1.     import java.util.*;
  2.     class vector {
  3.         public static void main(String args[]) {
  4.             Vector obj = new Vector(4,2);
  5.             obj.addElement(new Integer(3));
  6.             obj.addElement(new Integer(2));
  7.             obj.addElement(new Integer(5));
  8.             obj.removeAll(obj);
  9.             System.out.println(obj.isEmpty());
  10.         }
  11.     }

a) 0
b) 1
c) true
d) false
View Answer

Answer: c
Explanation: firstly elements 3, 2, 5 are entered in the vector obj, but when obj.removeAll(obj); is executed all the elements are deleted and vector is empty, hence obj.isEmpty() returns true.
Output:
$ javac vector.java
$ java vector
true

8. What is the output of this program?

  1.     import java.util.*;
  2.     class stack {
  3.         public static void main(String args[]) {
  4.             Stack obj = new Stack();
  5.             obj.push(new Integer(3));
  6.             obj.push(new Integer(2));
  7.             obj.pop();
  8.             obj.push(new Integer(5));
  9.      	    System.out.println(obj);
  10.         }
  11.     }

a) [3, 5]
b) [3, 2]
c) [3, 2, 5]
d) [3, 5, 2]
View Answer

Answer: a
Explanation: push() and pop() are standard functions of the class stack, push() inserts in the stack and pop removes from the stack. 3 & 2 are inserted using push() the pop() is used which removes 2 from the stack then again push is used to insert 5 hence stack contains elements 3 & 5.
Output:
$ javac stack.java
$ java stack
[3, 5]

9. What is the output of this program?

  1.     import java.util.*;
  2.     class hashtable {
  3.         public static void main(String args[]) {
  4.             Hashtable obj = new Hashtable();
  5.             obj.put("A", new Integer(3));
  6.             obj.put("B", new Integer(2));
  7.             obj.put("C", new Integer(8));
  8.             obj.remove(new String("A"));
  9.             System.out.print(obj);
  10.         }
  11.     }

a) {C=8, B=2}
b) [C=8, B=2]
c) {A=3, C=8, B=2}
d) [A=3, C=8, B=2]
View Answer

Answer: b
Explanation: None.
Output:
$ javac hashtable.java
$ java hashtable
{C=8, B=2}

10. What is the output of this program?

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

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.

About

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.


Sanfoundry Training Center of Excellence (CoE)

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.







Sanfoundry Internships
Subscribe Newsletter & Posts
Follow Manish & Sanfoundry