|
SCJP : NavigableMap Mock Exam Practice Questions
Questions no -1
What is the output for the below code ?
import java.util.NavigableMap;
import java.util.concurrent.ConcurrentSkipListMap;
public class Test {
public static void main(String... args) {
NavigableMap navMap = new
ConcurrentSkipListMap();
navMap.put(4, "April");
navMap.put(5, "May");
navMap.put(6, "June");
navMap.put(1, "January");
navMap.put(2, "February");
navMap.put(3, "March");
navMap.pollFirstEntry();
navMap.pollLastEntry();
navMap.pollFirstEntry();
System.out.println(navMap.size());
}
}
options
A)Compile error : No method name like pollFirstEntry() or pollLastEntry()
B)3
C)6
D)4
Correct answer is : B
Explanations :
pollFirstEntry() Removes and returns a key-value mapping associated with the least key in this map, or null if the map is empty.
pollLastEntry() Removes and returns a key-value mapping associated with the greatest key in this map, or null if the map is empty.
Questions no -2
What is the output for the below code ?
import java.util.NavigableMap;
import java.util.concurrent.ConcurrentSkipListMap;
public class Test {
public static void main(String... args) {
NavigableMap navMap = new
ConcurrentSkipListMap();
System.out.print(navMap.lastEntry());
}
}
options
A)Compile error : No method name like lastEntry()
B)null
C)NullPointerException
D)0
Correct answer is : B
Explanations : lastEntry() Returns a key-value mapping associated with the greatest key in this map, or null if the map is empty.
Questions no -3
What is the output for the below code ?
import java.util.NavigableMap;
import java.util.concurrent.ConcurrentSkipListMap;
public class Test {
public static void main(String... args) {
NavigableMapnavMap = new
ConcurrentSkipListMap();
navMap.put(4, "April");
navMap.put(5, "May");
navMap.put(6, "June");
navMap.put(1, "January");
navMap.put(2, "February");
System.out.print(navMap.ceilingKey(3));
}
}
options
A)Compile error : No method name like ceilingKey()
B)null
C)NullPointerException
D)4
Correct answer is : D
Explanations : Returns the least key greater than or equal to the given key, or null if there is no such key.
In the above case : 3 is not a key so return 4 (least key greater than or equal to the given key).
Questions no -4
What is the output for the below code ?
import java.util.NavigableMap;
import java.util.concurrent.ConcurrentSkipListMap;
public class Test {
public static void main(String... args) {
NavigableMapnavMap = new
ConcurrentSkipListMap();
navMap.put(4, "April");
navMap.put(5, "May");
navMap.put(6, "June");
navMap.put(1, "January");
navMap.put(2, "February");
System.out.print(navMap.floorKey(3));
}
}
options
A)Compile error : No method name like floorKey()
B)null
C)NullPointerException
D)2
Correct answer is : D
Explanations : Returns the greatest key less than or equal to the given key, or null if there is no such key.
In the above case : 3 is not a key so return 2 (greatest key less than or equal to the given key).
| |