Java Multiple Choice Questions & Answers on Java.util – LinkedList, HashSet & TreeSet Class for Freshers

https://www.computersprofessor.com/2017/12/java-multiple-choice-questions-answers_20.html
1. Which of these standard collection classes implements a linked list data structure?
a) AbstractList
b) LinkedList
c) HashSet
d) AbstractSet
Answer: b
2. Which of these classes implements Set interface?
a) ArrayList
b) HashSet
c) LinkedList
d) DynamicList
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()
Answer: d
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
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
Explanation: removeLast() and removeFirst() methods are used to remove elements in end and beginning of a linked list.
6. Which of these method is used to change an element in a LinkedList Object?
a) change()
b) set()
c) redo()
d) add()
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 is the output of this program?
import java.util.*;
class Linkedlist {
public static void main(String args[]) {
LinkedList obj = new LinkedList();
obj.add("A");
obj.add("B");
obj.add("C");
obj.addFirst("D");
System.out.println(obj);
}
}
a) [A, B, C]
b) [D, B, C]
c) [A, B, C, D]
d) [D, A, B, C]
Answer: d
Explanation: obj.addFirst(“D”) method is used to add ‘D’ to the start of a LinkedList object obj.
8. What is the output of this program?
import java.util.*;
class Linkedlist {
public static void main(String args[]) {
LinkedList obj = new LinkedList();
obj.add("A");
obj.add("B");
obj.add("C");
obj.removeFirst();
System.out.println(obj);
}
}
a) [A, B]
b) [B, C]
c) [A, B, C, D]
d) [A, B, C]
Answer: b
9. What is the output of this program?
import java.util.*;
class Output {
public static void main(String args[]) {
HashSet obj = new HashSet();
obj.add("A");
obj.add("B");
obj.add("C");
System.out.println(obj + " " + obj.size());
}
}
a) ABC 3
b) [A, B, C] 3
c) ABC 2
d) [A, B, C] 2
b) [A, B, C] 3
c) ABC 2
d) [A, B, C] 2
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.
10. What is the output of this program?
import java.util.*;
class Output {
public static void main(String args[]) {
TreeSet t = new TreeSet();
t.add("3");
t.add("9");
t.add("1");
t.add("4");
t.add("8");
System.out.println(t);
}
}
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
Explanation:TreeSet class uses set to store the values added by function add in ascending order using tree for storage