Java Questions & Answers – Java.util – LinkedList, HashSet & TreeSet Class

This set of Java Multiple Choice Questions & Answers (MCQs) focuses 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
View Answer

Answer: b
Explanation: None.

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

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

Answer: d
Explanation: None.
advertisement
advertisement

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

Answer: a
Explanation: 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()
View Answer

Answer: c
Explanation: 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()
View Answer

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

advertisement
  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]
View Answer

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

advertisement
$ 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]
View Answer

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

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

Answer: d
Explanation: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].

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.