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

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?

    Correct Answer: A

    The given class method 'checkValue' appears to compare the lengths of two strings, although the implementation shown in the question is incorrect. The correct form should use Integer.compare to compare the lengths of the strings, so it should return Integer.compare(s1.length(), s2.length()). Arrays.sort with CheckClass::checkValue is the correct way to sort the array based on this method, making option A the correct choice. This will result in the strings being sorted by their length, thereby printing 'Rat Cat Lion Tiger'.

Discussion
pul26Option: A

seem checkValue method is wrong. class CheckClass { public static int checkValue (String s1, String s2) { return Integer.compare(s1.length(), s2.length()); } } Answer is A

Svetleto13Option: A

A,tested

asdfjhfgjuaDCVOption: A

A is the correct answer

steefaandOption: A

Answer A, just replace "" in checkValue with - sign.