Free Java 17 OCP Practice Test 1

10 realistic exam questions for 1Z0-829 — with detailed explanations

10
Questions
20 min
Suggested Time
~65%
To Pass
Free
No Signup

📋 How This Practice Test Works

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.

Question 1

What is the output of the following code?

public class Main { sealed interface Shape permits Circle, Square {} record Circle(double radius) implements Shape {} record Square(double side) implements Shape {} public static String describe(Shape s) { return switch (s) { case Circle c -> "Circle: " + c.radius(); case Square sq -> "Square: " + sq.side(); }; } public static void main(String[] args) { System.out.println(describe(new Circle(5.0))); } }

✅ Correct Answer: A

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.

Question 2

Given the record below, what is the output?

public record Point(int x, int y) { public Point { if (x < 0 || y < 0) { throw new IllegalArgumentException("Negative"); } } } // In main: Point p = new Point(3, 4); System.out.println(p.x() + ", " + p.y());

✅ Correct Answer: B

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".

Question 3

What is the output?

import java.util.stream.*; import java.util.*; List<Integer> nums = List.of(1, 2, 3, 4, 5); int result = nums.stream() .filter(n -> n % 2 == 0) .mapToInt(Integer::intValue) .sum(); System.out.println(result);

✅ Correct Answer: B

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.

Question 4

Which statement about the Java Platform Module System (JPMS) is correct?

✅ Correct Answer: C

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.

Question 5

What is the output?

import java.util.function.*; Function<Integer, Integer> doubler = x -> x * 2; Function<Integer, Integer> addOne = x -> x + 1; Function<Integer, Integer> combined = doubler.andThen(addOne); System.out.println(combined.apply(3));

✅ Correct Answer: A

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.

Question 6

What is the output?

try { throw new RuntimeException("outer"); } catch (RuntimeException e) { try { throw new IllegalStateException("inner"); } catch (IllegalStateException e2) { e.addSuppressed(e2); } System.out.println(e.getSuppressed().length); }

✅ Correct Answer: B

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).

Question 7

What is true about the following code?

import java.nio.file.*; Path p = Paths.get("/data", "logs", "app.log"); Path normalized = p.normalize(); System.out.println(normalized);

✅ Correct Answer: A

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.

Question 8

What is the output?

import java.util.concurrent.*; ExecutorService executor = Executors.newFixedThreadPool(2); Future<Integer> future = executor.submit(() -> 42); System.out.println(future.get()); executor.shutdown();

✅ Correct Answer: A

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).

Question 9

Which option correctly demonstrates JDBC PreparedStatement usage?

✅ Correct Answer: C

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.

Question 10

What is the output?

import java.util.*; import java.util.stream.*; Optional<String> result = Stream.of("apple", "banana", "cherry") .filter(s -> s.startsWith("z")) .findFirst(); System.out.println(result.orElse("nothing"));

✅ Correct Answer: C

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".

Answered 0 of 10 questions

🎉 Test Complete!

0/10

Great work! Review your answers below.

Want 1,884 More OCP Questions?

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 Guide

From ₹1,400 / $16 • 6 months unlimited access