Browse Oracle Certification Guides

Java 21 1Z0-830 Sample Questions with Explanations

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.

Where these questions fit in the 1Z0-830 guide

The sample set below is part of the Oracle 1Z0-830 guide path:

1Z0-830 Java code-reading sample questions

Work through each prompt before opening the explanation. For Java professional questions, decide whether the code compiles before predicting output.


Question 1

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}
  • A. The method compiles and returns the first three characters for long strings.
  • B. The method compiles and returns the original object for non-strings.
  • C. The method does not compile because s is not in scope in the else block.
  • D. The method compiles but throws 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:

  • A ignores the invalid reference in the else block.
  • B assumes s exists even when the pattern did not match.
  • D predicts runtime behavior, but compilation fails first.

What this tests: Pattern variable scope and the compile-before-runtime habit.

Related topics: Pattern matching; Scope; Control flow; Compilation


Question 2

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"));
  • A. It prints 2 a.
  • B. It prints 2 none.
  • C. It throws an exception because the stream has already been operated on or closed.
  • D. It does not compile because 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:

  • A treats the stream like a reusable collection.
  • B assumes the second terminal operation sees an exhausted stream instead of an illegal reused stream.
  • D is wrong because the declarations compile; the failure is at runtime.

What this tests: Stream lifecycle, terminal operations, and runtime exception reasoning.

Related topics: Streams; Terminal operations; Runtime behavior; Exceptions


Question 3

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}
  • A. The compact constructor can validate and normalize constructor parameters before fields are assigned.
  • B. The compact constructor cannot assign to code because record components are final.
  • C. The record must explicitly assign this.code and this.seats.
  • D. The code does not compile because records cannot throw exceptions from constructors.

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:

  • B confuses reassigning the parameter with assigning the final field after initialization.
  • C describes a canonical constructor body, not a compact constructor.
  • D invents a restriction; constructors can throw exceptions.

What this tests: Record compact constructor rules and parameter normalization.

Related topics: Records; Constructors; Validation; Immutability


Question 4

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?

  • A. static void read(java.util.List<Number> values)
  • B. static void read(java.util.List<? extends Number> values)
  • C. static void read(java.util.List<? super Number> values)
  • D. 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:

  • A does not accept List<Integer> because Java generics are invariant.
  • C is useful when consuming numbers into a list, but it does not accept List<Integer>.
  • D also fails invariance for lists of narrower element types.

What this tests: Generic invariance and choosing an upper-bounded wildcard for read-only access.

Related topics: Generics; Wildcards; Invariance; Collections

Independent study note

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.

Revised on Sunday, May 10, 2026