10 exam-style questions for 1Z0-830 β featuring Virtual Threads, Record Patterns & Sequenced Collections
Test 1 focuses on what's new in Java 21: Virtual Threads, Pattern Matching for switch, Record Patterns, Sequenced Collections, and Stream API improvements.
Select an answer for each question, then click Submit & Show Answers to see your score with detailed explanations.
Executors.newVirtualThreadPerTaskExecutor() creates an executor where each submitted task runs on its own virtual thread. When the task runs, Thread.currentThread() returns a VirtualThread instance whose toString() includes "VirtualThread", so contains("VirtualThread") is true.
Virtual threads fully support Thread.sleep() β that's actually one of their key benefits (they don't block carrier threads during sleep).
Java 21's JEP 431 introduces the SequencedCollection interface, which List now extends. This adds addFirst(), addLast(), getFirst(), getLast(), and reversed() methods directly on the List interface.
After operations: list = [1, 2, 3, 4, 5]. getFirst() = 1, getLast() = 5, size() = 5.
Record patterns (Java 21) allow deconstruction of records directly in instanceof and switch. The pattern Point(int a, int b) matches the Point record and binds a to obj.x() (=3) and b to obj.y() (=4). Then a + b = 7.
This is one of the major new Java 21 OCP topics.
Java 21 finalized pattern matching for switch with type patterns and guarded patterns (the when clause). The cases are evaluated in order:
(1) i > 100: 42 > 100 is false β skip
(2) i > 10: 42 > 10 is true β match! Returns "medium: 42"
The order of guarded patterns matters β the first matching case wins.
Stream.toList() (added in Java 16, important for Java 21 OCP) returns an unmodifiable list β calling add(), remove(), etc. throws UnsupportedOperationException. Collectors.toList() returns a modifiable ArrayList (typically).
This is a key exam distinction β prefer Stream.toList() when you want an immutable result, use Collectors.toList() when you need to modify the list.
Lambdas (and inner classes) can only capture local variables that are effectively final β meaning they're never reassigned after initialization.
Here, counter++ is equivalent to counter = counter + 1, which reassigns counter. This violates the "effectively final" rule, so the code fails to compile.
To fix: use an AtomicInteger or an array of size 1.
Because Shape is a sealed interface, the compiler knows the complete set of permitted implementations (Circle, Square, Triangle). The switch covers all three, so it's exhaustive β no default case is needed.
This is one of the main benefits of sealed types + pattern matching.
Note: At runtime, passing null would throw NullPointerException, but the compiler doesn't require null handling unless you explicitly want to handle it with case null ->.
Files.readAllLines(path) reads the entire file into a List<String> in memory β fine for small files, but not for large ones. It throws checked IOException if the file doesn't exist or can't be read (assume try-catch in real code).
For large files, prefer Files.lines(path) which returns a Stream that must be closed in try-with-resources β that's the memory-efficient way.
Collectors.partitioningBy(predicate) divides elements into two groups based on a boolean predicate. Always produces a Map with exactly two keys: true and false.
Even numbers (2, 4, 6) β key true, 3 elements.
Odd numbers (1, 3, 5) β key false, 3 elements.
Result: "3 3". Note that partitioningBy ALWAYS has both keys, even if one group is empty.
Stream.mapMulti (Java 16+, key for Java 21 OCP) is an alternative to flatMap when you want to emit multiple elements per input. Instead of creating a Stream per element (like flatMap), you push elements directly to a Consumer.
For each input (1, 2, 3), the lambda pushes the number AND numberΓ10. Output order is preserved: [1, 10, 2, 20, 3, 30].
mapMulti is often more efficient than flatMap when emitting few elements per input.
Great work! Review your answers below.
You've experienced 10 questions. The full JavaOCP platform has 2,074 Java 21 OCP questions covering every exam topic β Virtual Threads, Record Patterns, Sequenced Collections, Pattern Matching, JPMS, and more.
That's 41 full certification tests for 1Z0-830, all with detailed explanations like the ones above.
Start 3-Day Free Trial β Java 17 vs Java 21 GuideFrom βΉ1,400 / $16 β’ 6 months unlimited access