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? Answer: b 2. Which of these classes implements Set interface? Answer: b 3. Which of these method is used to add an element to the start of a LinkedList object? Answer: d 4. Which of these method of HashSet class is used to add elements to its object? Answer: a 5. Which of these methods can be used to delete the last element in a LinkedList object? Answer: c 6. Which of this method is used to change an element in a LinkedList Object? Answer: b 7. What will be the output of the following Java code snippet? a) [A, B, C] 8. What will be the output of the following Java program? a) [A, B] 9. What will be the output of the following Java program? a) ABC 3 10. What will be the output of the following Java program? a) [1, 3, 5, 8, 9]
a) AbstractList
b) LinkedList
c) HashSet
d) AbstractSet
Clarification: None.
a) ArrayList
b) HashSet
c) LinkedList
d) DynamicList
Clarification: HashSet and TreeSet implements Set interface where as LinkedList and ArrayList implements List interface.
a) add()
b) first()
c) AddFirst()
d) addFirst()
Clarification: None.
a) add()
b) Add()
c) addFirst()
d) insert()
Clarification: None.
a) remove()
b) delete()
c) removeLast()
d) deleteLast()
Clarification: removeLast() and removeFirst() methods are used to remove elements in end and beginning of a linked list.
a) change()
b) set()
c) redo()
d) add()
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.
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);
}
}
b) [D, B, C]
c) [A, B, C, D]
d) [D, A, B, C]
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].
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);
}
}
b) [B, C]
c) [A, B, C, D]
d) [A, B, C]
Clarification: None.
Output:
$ javac Linkedlist.java
$ java Linkedlist
[B, C]
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());
}
}
b) [A, B, C] 3
c) ABC 2
d) [A, B, C] 2
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
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);
}
}
b) [3, 4, 1, 8, 9]
c) [9, 8, 4, 3, 1]
d) [1, 3, 4, 8, 9]
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].