Java OCA OCP Practice Question 2400

Question

Given the following code, which code options when inserted at //INSERT CODE HERE will sort the keys in props?

class Main {/*  ww w.j a va 2 s  .  com*/
    public static void main(String... args) {
        HashMap props = new HashMap();
        props.put("A", "a");
        props.put("B", "b");
        props.put("C", "c");
        Set keySet = props.keySet();
        //INSERT CODE HERE
    }
}
  • a Arrays.sort(keySet);
  • b Collections.sort(keySet);
  • c Collection.sort(keySet);
  • d Collections.arrange(keySet);
  • e keySet = new TreeSet(keySet);
  • f keySet = new SortedSet(keySet);


e

Note

Option (a) is incorrect because method sort() of class Arrays sorts only arrays, not HashMap.

Option (b) is incorrect.

Method sort() of class Collections sorts List objects, not HashMap.

Option (c) is incorrect because Collection isn't defined in the Java API.

Option (d) is incorrect because method arrange() isn't defined in class Collections.

Option (f) is incorrect because SortedSet is an interface, which can't be instantiated.




PreviousNext

Related