nerdexam
Oracle

1Z0-809 · Question #77

Given: class CheckClass { public static int checkValue (String s1, String s2) { return s1.length() ?s2.length(); } } and the code fragment: String[] strArray = new String [] {"Tiger", "Rat", "Cat"…

The correct answer is A. Arrays.sort(strArray, CheckClass :: checkValue). Option A works because checkValue is a static method with the signature (String, String) → int, which exactly matches the Comparator<String> functional interface. The syntax ClassName::staticMethodName is the correct method reference form for static methods, so…

Question

Given: class CheckClass { public static int checkValue (String s1, String s2) { return s1.length() ?s2.length(); } } and the code fragment: String[] strArray = new String [] {"Tiger", "Rat", "Cat", "Lion"} //line n1 for (String s : strArray) { System.out.print (s + " "); } Which code fragment should be inserted at line n1 to enable the code to print Rat Cat Lion Tiger?

Options

  • AArrays.sort(strArray, CheckClass :: checkValue);
  • BArrays.sort(strArray, (CheckClass :: new) :: checkValue);
  • CArrays.sort(strArray, (CheckClass :: new).checkValue);
  • DArrays.sort(strArray, CheckClass :: new :: checkValue);

How the community answered

(39 responses)
  • A
    74% (29)
  • B
    15% (6)
  • C
    3% (1)
  • D
    8% (3)

Explanation

Option A works because checkValue is a static method with the signature (String, String) → int, which exactly matches the Comparator<String> functional interface. The syntax ClassName::staticMethodName is the correct method reference form for static methods, so Arrays.sort(strArray, CheckClass::checkValue) sorts the array by ascending string length (Rat=3, Cat=3, Lion=4, Tiger=5), producing the expected output.

Options B and D are wrong because Java does not allow chaining :: operators - (CheckClass::new)::checkValue and CheckClass::new::checkValue are both syntactically illegal; a constructor reference is a functional interface value, not an object you can further dereference with ::. Option C fails for the same reason: (CheckClass::new) produces a functional interface reference, not an actual instance, so you cannot call .checkValue on it with dot notation.

Memory tip: For static method references, think Class::method - no new, no chaining. If you see ::new, that's a constructor reference (e.g., ArrayList::new), and it cannot be combined with another :: or . to reach a method.

Community Discussion

No community discussion yet for this question.

Full 1Z0-809 Practice