1Z0-829 Working with Arrays and Collections Practice Question
A developer is writing a method that takes a Collection<Integer> and returns a List<Integer> containing the same elements in sorted order. The method should not modify the original collection. The developer tries the following code: public List<Integer> sortCollection(Collection<Integer> col) { return col.stream().sorted().collect(Collectors.toList()); } The code compiles and runs, but the team lead says it is not optimal. What improvement should be made?
⚠ Common exam trap
The trap here is that candidates often focus on performance improvements like parallelism or sorting efficiency, but the actual optimization is about immutability and API design, which is a common subtlety in Java collections questions.
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
✓
Return an unmodifiable list using collect(Collectors.toUnmodifiableList())
The current code creates a mutable list via `Collectors.toList()`, but the method contract does not require mutability. Using `Collectors.toUnmodifiableList()` returns an unmodifiable list, which is more memory-efficient and thread-safe, and it aligns with the principle of returning immutable collections when modification is not needed. This improvement does not affect sorting correctness but optimizes the result for immutability.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Use parallelStream() for better performance
Why it's wrong here
Overhead of parallel may outweigh benefit for small collections.
- ✗
Use a TreeSet and then convert to List
Why it's wrong here
TreeSet removes duplicates, which may not be desired.
- ✗
Convert the collection to an ArrayList first, then call Collections.sort() on it
Why it's wrong here
This creates an extra intermediate list and modifies it, not necessarily better.
- ✓
Return an unmodifiable list using collect(Collectors.toUnmodifiableList())
Why this is correct
Ensures result cannot be modified, good practice for API design.
Go deeper
Related to this question
About these practice questions
Courseiva writes every 1Z0-829 question from scratch — 513 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or 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.