Java OCA OCP Practice Question 3060

Question

Given:

import java.util.NavigableMap;
import java.util.TreeMap;

public class Main {
   public static void main(String[] args) {
      TreeMap<String, String> m1 = new TreeMap<String, String>();
      m1.put("a", "amy");
      m1.put("f", "frank");
      NavigableMap<String, String> m2 = m1.descendingMap();
      try {/* w ww .  jav  a2s  .c  o m*/
         m1.put("j", "john");
         m2.put("m", "mary");
      } catch (Exception e) {
         System.out.print("ex ");
      }
      m1.pollFirstEntry();
      System.out.println(m1 + "\n" + m2);
   }
}

What is the result?

  • A. {f=frank, j=john} {f=frank}
  • B. {f=frank, j=john} {m=mary, f=frank}
  • C. ex {f=frank, j=john} {f=frank}
  • D. {f=frank, j=john, m=mary} {m=mary, j=john, f=frank}
  • E. ex {f=frank, j=john, m=mary} {f=frank}
  • F. ex {f=frank, j=john, m=mary} {f=frank, a=amy}
  • G. {a=amy, f=frank, j=john, m=mary} {f=frank, a=amy}
  • H. Compilation fails due to error(s) in the code.


D is correct.

Note

The descendingMap() method creates a NavigableMap in reverse order, which is "backed" to the map it was created from.

It is NOT a subMap, so there is no worry about being outside of its range.

The pollFirstEntry() method returns AND removes the "first" entry in the Map.




PreviousNext

Related