250+ TOP MCQs on Java.util – LinkedList, HashSet & TreeSet Class and Answers

This set of Java online quiz on “Java.util – LinkedList, HashSet & TreeSet Class”.

1. Which of these standard collection classes implements a linked list data structure?
a) AbstractList
b) LinkedList
c) HashSet
d) AbstractSet

Answer: b
Clarification: None.

2. Which of these classes implements Set interface?
a) ArrayList
b) HashSet
c) LinkedList
d) DynamicList

Answer: b
Clarification: HashSet and TreeSet implements Set interface where as LinkedList and ArrayList implements List interface.

3. Which of these method is used to add an element to the start of a LinkedList object?
a) add()
b) first()
c) AddFirst()
d) addFirst()

Answer: d
Clarification: None.

4. Which of these method of HashSet class is used to add elements to its object?
a) add()
b) Add()
c) addFirst()
d) insert()

Answer: a
Clarification: None.

5. Which of these methods can be used to delete the last element in a LinkedList object?
a) remove()
b) delete()
c) removeLast()
d) deleteLast()

Answer: c
Clarification: removeLast() and removeFirst() methods are used to remove elements in end and beginning of a linked list.

6. Which of this method is used to change an element in a LinkedList Object?
a) change()
b) set()
c) redo()
d) add()

Answer: b
Clarification: An element in a LinkedList object can be changed by first using get() to obtain the index or location of that object and the passing that location to method set() along with its new value.

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

  1.     import java.util.*;
  2.     class Linkedlist 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             LinkedList obj = new LinkedList();
  7.             obj.add("A");
  8.             obj.add("B");
  9.             obj.add("C");
  10.             obj.addFirst("D");
  11.             System.out.println(obj);
  12.         }
  13.     }

a) [A, B, C]
b) [D, B, C]
c) [A, B, C, D]
d) [D, A, B, C]

Answer: d
Clarification: obj.addFirst(“D”) method is used to add ‘D’ to the start of a LinkedList object obj.
Output:

$ javac Linkedlist.java
$ java Linkedlist
[D, A, B, C].

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

  1.     import java.util.*;
  2.     class Linkedlist 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             LinkedList obj = new LinkedList();
  7.             obj.add("A");
  8.             obj.add("B");
  9.             obj.add("C");
  10.             obj.removeFirst();
  11.             System.out.println(obj);
  12.         }
  13.     }

a) [A, B]
b) [B, C]
c) [A, B, C, D]
d) [A, B, C]

Answer: b
Clarification: None.
Output:

$ javac Linkedlist.java
$ java Linkedlist
[B, C]

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

  1.     import java.util.*;
  2.     class Output 
  3.     {
  4.         public static void main(String args[]) 
  5.         {
  6.             HashSet obj = new HashSet();
  7.             obj.add("A");
  8.             obj.add("B");
  9.             obj.add("C");
  10.             System.out.println(obj + " " + obj.size());
  11.         }
  12.     }

a) ABC 3
b) [A, B, C] 3
c) ABC 2
d) [A, B, C] 2

Answer: b
Clarification: HashSet obj creates an hash object which implements Set interface, obj.size() gives the number of elements stored in the object obj which in this case is 3.
Output:

$ javac Output.java
$ java Output
[A, B, C] 3

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

  1.     import java.util.*; 
  2.     class Output 
  3.     {
  4.         public static void main(String args[]) 
  5.         { 
  6.             TreeSet t = new TreeSet();
  7.             t.add("3");
  8.             t.add("9");
  9.             t.add("1");
  10.             t.add("4");
  11.             t.add("8"); 
  12.             System.out.println(t);
  13.         }
  14.     }

a) [1, 3, 5, 8, 9]
b) [3, 4, 1, 8, 9]
c) [9, 8, 4, 3, 1]
d) [1, 3, 4, 8, 9]

Answer: d
Clarification:TreeSet class uses set to store the values added by function add in ascending order using tree for storage
Output:

$ javac Output.java
$ java Output
[1, 3, 4, 8, 9].

Leave a Reply

Your email address will not be published. Required fields are marked *