Given:


import java.util.*;   
public class Magellan {   
  public static void main(String[] args) {   
    TreeMap myMap = new TreeMap();   
    myMap.put("a", "apple"); myMap.put("d", "date");   
    myMap.put("f", "fig"); myMap.put("p", "pear");   
    System.out.println("1st after mango: " +   // sop 1  
       myMap.higherKey("f"));  
     System.out.println("1st after mango: " +   // sop 2  
       myMap.ceilingKey("f"));  
     System.out.println("1st after mango: " +   // sop 3  
       myMap.floorKey("f"));  
     SortedMap sub = new TreeMap();  
     sub = myMap.tailMap("f");  
     System.out.println("1st after mango: " +   // sop 4  
       sub.firstKey());  
   }  
 }

Which of the System.out.println statements will produce the output 1st after mango: p?

  1. sop 1

  2. sop 2

  3. sop 3

  4. sop 4

  5. compilation fails


Correct Option: A
Explanation:

To solve this question, we need to understand the methods used in the given Java code. TreeMap is a class that implements the Map interface using a tree structure. It stores key-value pairs in a sorted order. The methods used in the given code are:

  • higherKey(Object key): Returns the least key strictly greater than the given key, or null if there is no such key.
  • ceilingKey(Object key): Returns the least key greater than or equal to the given key, or null if there is no such key.
  • floorKey(Object key): Returns the greatest key less than or equal to the given key, or null if there is no such key.
  • tailMap(K fromKey): Returns a view of the portion of this map whose keys are greater than or equal to fromKey.

Now, let's go through each option and determine which one will produce the output "1st after mango: p":

A. sop 1: This statement calls the higherKey() method on the TreeMap object with the argument "f". This returns the least key strictly greater than "f", which is "p". The output of this statement will be "1st after mango: p". Therefore, option A is correct.

B. sop 2: This statement calls the ceilingKey() method on the TreeMap object with the argument "f". This returns the least key greater than or equal to "f", which is "f" itself. The output of this statement will be "1st after mango: f". Therefore, option B is incorrect.

C. sop 3: This statement calls the floorKey() method on the TreeMap object with the argument "f". This returns the greatest key less than or equal to "f", which is "f" itself. The output of this statement will be "1st after mango: f". Therefore, option C is incorrect.

D. sop 4: This statement creates a new TreeMap object named sub using the tailMap() method on the existing TreeMap object myMap. The tailMap() method returns a view of the portion of this map whose keys are greater than or equal to "f". The sub.firstKey() method returns the first key in the sub map, which is "f". The output of this statement will be "1st after mango: f". Therefore, option D is incorrect.

E. compilation fails: There are no compilation errors in the given code. Therefore, option E is incorrect.

The Answer is: A. sop 1

Find more quizzes: