Oracle Certified Professional Java Se 8 Programmer · Free Practice Question Medium

Question 33

Given code of Test.java file: 

  • A {32=Mumbai, 39=Chennai}
  • B {11=Sri Nagar, 25=Pune}
  • C {25=Pune, 32=Mumbai, 39=Chennai}
  • D {11=Sri Nagar}
Reveal correct answer

Correct answer: B

Explanation

TreeMap is sorted map based on the natural ordering of keys. So, map has entries: {11=Sri Nagar, 25=Pune, 32=Mumbai, 39=Chennai}. 


headMap(K toKey, boolean inclusive) => returns the map till toKey, if inclusive is true. Hence the output is: {11=Sri Nagar, 25=Pune}. 


For the exam, you should know some of the methods from NavigableMap map. Below are the method calls and outputs for the map object used in this example: 


//NavigableMap<K,V> tailMap(K fromKey, boolean inclusive); => Returns a view of the portion of this map whose keys are greater than (or equal to, if 'inclusive' is true) fromKey. 

System.out.println(map.tailMap(25, true)); //{25=Pune, 32=Mumbai, 39=Chennai} 


//Map.Entry<K,V> firstEntry(); => Returns a key-value mapping associated with the least key in this map. 

System.out.println(map.firstEntry()); //11=Sri Nagar 


//Map.Entry<K,V> lastEntry(); => Returns a key-value mapping associated with the greatest key in this map. 

System.out.println(map.lastEntry()); //39=Chennai 


//NavigableMap<K,V> descendingMap(); => Returns a reverse order view of the mappings contained in this map. 

System.out.println(map.descendingMap()); //{39=Chennai, 32=Mumbai, 25=Pune, 11=Sri Nagar} 


//K floorKey(K key); => Returns the greatest key less than or equal to the given key. 

System.out.println(map.floorKey(30)); //25 


//K ceilingKey(K key); => Returns the least key greater than or equal to the given key. 

System.out.println(map.ceilingKey(30)); //32 

Discussion

Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.

You must be logged in to post a comment.

Preparing For

Your Certification?

255+ certifications
Detailed explanations
Free PDF samples

Has All The Questions You Need