SCJP Module-9 Question-2


 

SCJP Module-9 Question-2

The sample program given below will test your knowledge of List, NavigableSet, TreeSet of collection framework in Java.

The sample program given below will test your knowledge of List, NavigableSet, TreeSet of collection framework in Java.

Given below the sample code :

import java.util.ArrayList;
import java.util.List;
import java.util.NavigableSet;
import java.util.TreeSet;
public class CheckTail{
public static void main(String... args) {
List<Integer> MyList = new ArrayList<Integer>();
MyList.add(21);
MyList.add(3);
MyList.add(3);
MyList.add(3);
MyList.add(30);
MyList.add(2);
NavigableSet<Integer> ngs = new TreeSet<Integer>(MyList);
System.out.println(ngs.tailSet(3));
}
}

What will be the output of the above code ?

1. Compilation error

2. [21, 3, 30, 2]

3. [3, 30, 2]

4. [ 3, 21, 30]

Answer

(4)

Explanation

The ' tailSet(3) ' returns elements which are greater than or equal to 3.

 

 

Ads