Java 21 1Z0-830 sample questions with explanations, traps, topic labels, and IT Mastery route links.
These original sample questions are designed to help you check how the exam topics appear in decision-style prompts. They are not taken from the live exam.
Use these sample questions as a guided self-assessment for Oracle Java SE 21 Developer Professional (1Z0-830) topics such as type inference, pattern matching, records, sealed types, collections, streams, modules, concurrency, I/O, and exception flow. The prompts focus on code-reading precision: compile-time legality first, then runtime behavior.
The sample set below is part of the Oracle 1Z0-830 guide path:
Work through each prompt before opening the explanation. For Java professional questions, decide whether the code compiles before predicting output.
Topic: Pattern variable scope
What is the result of compiling and running this code?
1static String label(Object value) {
2 if (value instanceof String s && s.length() > 3) {
3 return s.substring(0, 3);
4 } else {
5 return s;
6 }
7}
s is not in scope in the else block.ClassCastException for non-strings.Best answer: C
Explanation: The pattern variable s is definitely matched only in the true branch where the pattern and length test succeed. It is not available in the else branch, so the method fails compilation.
Why the other choices are weaker:
else block.s exists even when the pattern did not match.What this tests: Pattern variable scope and the compile-before-runtime habit.
Related topics: Pattern matching; Scope; Control flow; Compilation
Topic: Stream reuse and terminal operations
What happens when the following code runs?
1var stream = java.util.stream.Stream.of("a", "bb", "ccc");
2var count = stream.filter(s -> s.length() > 1).count();
3var first = stream.findFirst();
4System.out.println(count + " " + first.orElse("none"));
2 a.2 none.filter changes the declared type of stream.Best answer: C
Explanation: A stream is single-use. The count() call is a terminal operation. Reusing the same stream reference for findFirst() causes an IllegalStateException.
Why the other choices are weaker:
What this tests: Stream lifecycle, terminal operations, and runtime exception reasoning.
Related topics: Streams; Terminal operations; Runtime behavior; Exceptions
Topic: Record constructor validation
Which statement about this record is correct?
1record Course(String code, int seats) {
2 Course {
3 if (seats < 0) throw new IllegalArgumentException();
4 code = code.strip().toUpperCase();
5 }
6}
code because record components are final.this.code and this.seats.Best answer: A
Explanation: In a compact constructor, the component parameters are in scope and can be validated or reassigned before the implicit field assignment occurs. The record fields themselves remain final after construction.
Why the other choices are weaker:
What this tests: Record compact constructor rules and parameter normalization.
Related topics: Records; Constructors; Validation; Immutability
Topic: Generic invariance
Which declaration allows the method to read numbers from a list of Integer, Long, or Double values without adding new values to the list?
static void read(java.util.List<Number> values)static void read(java.util.List<? extends Number> values)static void read(java.util.List<? super Number> values)static void read(java.util.List<Object> values)Best answer: B
Explanation: ? extends Number is the producer-style bound. It accepts lists whose element type is Number or a subtype, and values can be read as Number.
Why the other choices are weaker:
List<Integer> because Java generics are invariant.List<Integer>.What this tests: Generic invariance and choosing an upper-bounded wildcard for read-only access.
Related topics: Generics; Wildcards; Invariance; Collections
Tech Exam Lexicon and IT Mastery are independent study tools. They are not affiliated with, endorsed by, or sponsored by Oracle or any certification body.