What Happens When You Sort a List Created with List.of()?
Given the code fragment: What is the result? - 
Community Votes
100% of anonymous learners picked answer C. Votes are pick records left by other test-takers — they are not the verified answer.
Community Insight
It tests immutable collection factories versus mutability, and the trap is assuming List.of() behaves like new ArrayList<>() so the comparator's hue-then-value ordering actually gets applied.
This 1Z0-819 fragment builds a flower list with List.of() and then attempts to sort it with a Comparator, so the exam tests whether you recognize that List.of() returns an unmodifiable list. The result is not a sorted printout but a runtime UnsupportedOperationException, making option C the answer.
The most common wrong pick is option A, [Red:75, Red:100, Yellow:50, Yellow:75], because candidates mentally swap List.of() for an ArrayList and apply the hue-then-value Comparator successfully, never noticing the list cannot be structurally or element-wise modified.
Community Discussion (4 comments)
Comments & Corrections
No comments yet — spotted an error or have a note? Share it below.
Expert Analysis
Why the Answer Is Correct
List.of(...) is a Java 9+ factory that returns an instance of ImmutableCollections.ListN, a fully immutable list: add, remove, set, replaceAll and sort all throw UnsupportedOperationException. The call to sort on the variable clrs therefore compiles fine (it is a legal List method) but fails at runtime, which is exactly what option C states. Because the exception aborts the program before any printing occurs, none of the bracketed orderings in options A, B, D or E can ever be produced. As ASPushkin put it, "List.of creates unmodifiable list you can’t sort this list" and the call throws "java.lang.UnsupportedOperationException" — matching the community's unanimous vote for C.Why the Other Options Are Wrong
Option A would be the output only if the collection were mutable, for examplenew ArrayList<>(List.of(...)): sorting by hue then value gives Red:75, Red:100, Yellow:50, Yellow:75, which is precisely why A is the seductive distractor. Option E reverses that ordering (descending), option D sorts by hue but mixes the value order, and option B is an arbitrary ordering that no natural comparator on hue/value would produce. None of them can appear here because the exception is thrown before the list is ever printed.Community Comment Notes
Every commenter agrees the underlying cause is immutability rather than comparator logic. ASPushkin explains that sorting the immutable list throws anUnsupportedOperationException and adds that "in the case of reqular List the answer is A", which is the key contrast the exam is testing. d7bb0b2 reiterates that "List.of is an inmutable cannot add, sort or modified", and Felipe47 simply confirms that "List.of is a List inmmutable". These observations line up with the JDK behaviour in ImmutableCollections.AbstractImmutableList, so the community consensus and the specification agree.Exam Strategy Tip
When a 1Z0-819 question shows a collection being created, read the factory first:List.of/Set.of/Map.of are immutable, Arrays.asList is fixed-size (set allowed, add/remove forbidden), and new ArrayList<> is fully mutable. Then check whether the following code mutates the collection, because that single decision usually eliminates half the answer choices. Official Reference
Exam Strategy
Identify the collection factory before reading the operation: List.of() yields an unmodifiable list, so any sort/add/remove/set call on it throws UnsupportedOperationException, not a sorted result. If the code used new ArrayList<>(...), the comparator's hue-then-value ordering would apply and option A would be correct, so always resolve mutability first.
Frequently Asked Questions
Why does sorting the List.of list throw UnsupportedOperationException?
List.of returns an immutable list, and List.sort() must replace elements in place; since the list cannot be modified, the JDK throws UnsupportedOperationException at runtime.
What would the output be if the flowers were in a new ArrayList instead?
Option A, [Red:75, Red:100, Yellow:50, Yellow:75], because a mutable list can actually be sorted by hue and then by value.