SCJP Module-9 Question-3


 

SCJP Module-9 Question-3

The Sample program given below will check your knowledge of Collection framework in Java.

The Sample program given below will check your knowledge of Collection framework in Java.

Given below the sample code :

import java.util.NavigableMap;
import java.util.concurrent.ConcurrentSkipListMap;
public class CheckSize {
public static void main(String... args) {
NavigableMap <Integer, String>nM = new ConcurrentSkipListMap<Integer, String>();
nM.put(4, "Wednesday");
nM.put(5, "Thursday");
nM.put(6, "Friday");
nM.put(1, "Sunday");
nM.put(2, "Monday");
nM.put(3, "Tuesday");
nM.pollFirstEntry();
nM.pollLastEntry();
nM.pollFirstEntry();
nM.pollLastEntry();
System.out.println(nM.size());
}
}

What will be the output of the above code ?

1. Compile error

2. 6

3. 3

4. 2

Answer

(4)

Explanation

pollFirstEntry()   It Removes the element and returns a key-value mapping
associated with the least key in this map, or null if the map is empty.

pollLastEntry()   It Removes the element and returns a key-value mapping
associated with the greatest key in this map, or null ifthe map is empty.

Ads