1Z0-829 Working with Arrays and Collections Practice Question
Which of the following correctly sorts an array of integers (int[] arr) in descending order?
⚠ Common exam trap
Many exam-takers assume `Arrays.sort()` with a `Comparator` works on primitive arrays, but the Java API explicitly separates primitive and object array sorting, and the `Comparator` overload is only available for object arrays.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
Integer[] boxed = Arrays.stream(arr).boxed().toArray(Integer[]::new); Arrays.sort(boxed, Collections.reverseOrder()); arr = Arrays.stream(boxed).mapToInt(i->i).toArray();
It demonstrates the proper technique for sorting a primitive int array in descending order. The `Arrays.sort()` method does not accept a `Comparator` for primitive arrays, so the array must first be boxed into an `Integer[]` using `Arrays.stream(arr).boxed().toArray(Integer[]::new)`. After sorting with `Collections.reverseOrder()`, the result is unboxed back to `int[]` via `mapToInt(i->i).toArray()`. This approach correctly leverages the `Comparator` interface, which only works with object types.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
Integer[] boxed = Arrays.stream(arr).boxed().toArray(Integer[]::new); Arrays.sort(boxed, Collections.reverseOrder()); arr = Arrays.stream(boxed).mapToInt(i->i).toArray();
Why this is correct
Correct: this boxes the array, sorts descending, and unboxes back to int[].
- ✗
Arrays.sort(arr, Collections.reverseOrder());
Why it's wrong here
Incorrect: Arrays.sort with Comparator only works for object arrays, not primitive int[].
- ✗
None of the above, because primitive arrays cannot be sorted in descending order.
Why it's wrong here
Incorrect: descending sort is possible by using boxed arrays.
- ✗
Arrays.sort(arr, (a,b) -> b - a);
Why it's wrong here
Incorrect: the lambda cannot be applied to int[] directly; requires Integer[].
- ✗
Arrays.parallelSort(arr, (a,b) -> b - a);
Why it's wrong here
Incorrect: parallelSort also requires object array for Comparator.
Go deeper
Related to this question
About these practice questions
This 1Z0-829 question is part of Courseiva's 513-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This 1Z0-829 practice question is part of Courseiva's free Oracle certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the 1Z0-829 exam.