Exam 1z0-809 All QuestionsBrowse all questions from this exam
Question 246

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 + " ");

}

Rat Cat Lion Tiger?

Which code fragment should be inserted at line n1 to enable the code to print

    Correct Answer: A

    The correct way to sort the array using the static method checkValue from the CheckClass is to use method references. The correct method reference for a static method is ClassName::methodName. Therefore, the correct code fragment to insert at line n1 is Arrays.sort(strArray, CheckClass::checkValue). This will sort the array based on the lengths of the strings, enabling the code to print the correct order: Rat Cat Lion Tiger.

Discussion
adnano1234Option: A

A. Arrays.sort(strArray, CheckClass : : checkValue);

steefaandOption: A

A is true.

iSnoverOption: A

The answer is A. Arrays.sort(strArray, CheckClass : : checkValue);