10 realistic exam questions for 1Z0-829 — with detailed explanations
Select your answer for each question by clicking an option. When done with all 10 questions, click Submit & Show Answers at the bottom.
You'll see your score plus detailed explanations for every question — including why wrong answers are wrong.
These questions mirror the actual OCP exam style and difficulty. The real exam has 50 questions in 90 minutes.
The switch expression uses pattern matching (introduced in Java 21, but available as preview in Java 17). Because Shape is a sealed interface that only permits Circle and Square, the compiler knows the switch is exhaustive — no default case needed. The Circle pattern matches and binds c, so c.radius() returns 5.0.
The compact constructor public Point { ... } validates parameters before the canonical constructor assigns them. Since x=3 and y=4 are both non-negative, no exception is thrown. Record accessor methods are x() and y() (not getX()/getY()). The output is "3, 4".
The filter keeps only even numbers: 2 and 4. The mapToInt converts the Stream<Integer> to IntStream. The sum() terminal operation returns 2 + 4 = 6.
requires transitive M; means that any module reading the current module will also implicitly read module M. This is "implied readability".
Why others are wrong: (A) module-info.java must be in the root of the module source, not META-INF. (B) requires and exports are separate directives. (D) opens allows deep reflection at runtime, while exports only allows compile-time access to public types.
f.andThen(g) means apply f first, then g. So doubler.andThen(addOne).apply(3) → addOne.apply(doubler.apply(3)) → addOne.apply(6) → 7. Note this is different from compose, which applies in reverse order.
addSuppressed(Throwable) attaches the inner exception to the outer one as a suppressed exception. getSuppressed() returns an array of suppressed throwables, which contains exactly 1 element (the IllegalStateException).
Paths.get(String, String...) creates a Path object — it doesn't access the file system. normalize() only removes redundant elements like ".." and "." — since this path has no redundancies, it returns the same logical path. Path operations like get() and normalize() never throw IOException because they only manipulate path strings, not the file system.
executor.submit() accepts a Callable<V>, and the lambda () -> 42 is compatible with Callable<Integer> (returns a value). The future.get() call blocks until the task completes, then returns the value 42. The blocking call also handles checked exceptions InterruptedException and ExecutionException (assume try-catch or throws in actual exam scenarios).
JDBC parameter indices are 1-based, not 0-based. This is a common exam trap.
Why others are wrong: (A) Index starts at 1. (B) Commit is for transactions, not individual queries. (D) PreparedStatement IS the main protection against SQL injection in Java — that's its core benefit.
No string in the stream starts with "z", so filter produces an empty stream and findFirst() returns Optional.empty(). The orElse("nothing") method returns the default value when the Optional is empty. Output: "nothing".
Great work! Review your answers below.
You just experienced 10 questions. The full JavaOCP platform has 1,884 Java 17 OCP questions (1Z0-829) and 2,074 Java 21 OCP questions (1Z0-830).
That's 37 full certification tests for Java 17 plus 41 for Java 21 — all with detailed explanations.
Start 3-Day Free Trial → Java 17 vs Java 21 GuideFrom ₹1,400 / $16 • 6 months unlimited access