10 more challenging exam questions for 1Z0-829 — with detailed explanations
Test 2 covers different topics than Test 1: generics, polymorphism, collections, method references, and tricky exam patterns. Click an answer for each question.
When done, click Submit & Show Answers to see your score and detailed explanations for every question.
These questions reflect actual OCP exam difficulty. Aim for 65%+ to be exam-ready.
An upper-bounded wildcard List<? extends Number> is read-only for writes. You cannot add any element (except null) because the compiler can't guarantee type safety — the list could be List<Integer>, List<Long>, or any other Number subtype. Line 1 fails to compile. Line 2 is fine because we can safely read elements as the upper bound (Number).
Fields are not polymorphic — they're resolved by the reference type (Animal), so a.name returns "Animal". Methods are polymorphic — they're resolved by the actual object type (Dog), so a.getName() returns "Dog". This is a classic OCP trap.
Collectors.groupingBy with a downstream collector counting() groups by first character and counts elements in each group. The result is: {A=2, B=1, C=1}. Both "Alice" and "Anna" start with 'A', so grouped.get('A') returns 2L (boxed to Long for display).
Function<String, Integer>?
Function<String, Integer> requires a method that takes a
String and returns an Integer.
(A) String::length is VALID. It effectively translates to:
(String s) -> s.length(). The return type is int,
which is autoboxed to Integer.
(B) Integer::parseInt is VALID. It references
the static method Integer.parseInt(String s), which takes a
String and returns an int.
(C) String::hashCode is VALID. It effectively
translates to:
(String s) -> s.hashCode(), returning an int.
(D) System.out::println is INVALID because
println returns void, not Integer.
It is compatible with Consumer<String>, not
Function<String, Integer>.
First line: "Hello" literal is interned in the String pool. The variable s also references that interned literal. So s == "Hello" is true (same reference).
Second line: sb.toString() returns "Hello" (content equal to s). String.equals() compares content, so the result is true.
The switch expression with arrow syntax -> uses yield when the case branch is a block. The case 4, 5, 6 matches x=5, executes the block, and yields "medium".toUpperCase() which is "MEDIUM".
count is static — shared across all instances. Three constructor calls increment it three times, so count = 3. instanceCount is an instance variable — each Counter has its own. For c3 (just created), instanceCount was initialized to 0, then the constructor incremented it to 1.
The inner try throws RuntimeException("first"). The inner catch wraps it: new IllegalStateException("second", e) — "second" becomes the message, e becomes the cause. The outer catch receives this IllegalStateException. e.getMessage() returns "second", and e.getCause().getMessage() returns "first" (the original wrapped exception's message).
This is FALSE. Subclasses of a sealed class must be in the same module as the sealed class (or in the same package if the sealed class is in the unnamed module). They cannot be in any arbitrary module.
The other statements are TRUE: (A) Permits clause is required (except for nested classes in the same file). (B) Subclasses must specify their inheritance behavior. (D) Sealed type hierarchies allow the compiler to verify exhaustive pattern matching.
map.merge(key, value, remappingFunction): if the key is absent or maps to null, associates it with value. Otherwise replaces with remappingFunction.apply(oldValue, value).
For "a" (existing, value=1): merge applies Integer.sum(1, 10) = 11.
For "d" (absent): merge stores 100 directly without calling the function.
Great work! Review your answers below.
You've completed Test 2. The full JavaOCP platform has 1,884 Java 17 OCP questions across 37 timed certification tests — every topic, every difficulty level.
Plus 2,074 Java 21 OCP questions if you're targeting the newer exam.
Start 3-Day Free Trial → How to Pass First AttemptFrom ₹1,400 / $16 • 6 months unlimited access