OCP exam prep: built-in functional interfaces¶
☕ Java 17 · 🎓 OCP 1Z0-829 · The Ready-Made Lambda Toolkit
Optional page
This page is for readers preparing the Oracle Certified Professional Java SE 17 Developer exam (1Z0-829). The concepts are all you need otherwise.
Exam objective: Use functional interfaces from java.util.function, including primitive specializations.
Seven traps to watch for¶
Trap 1 — Know the method names cold
get(), accept(), test(), apply(). Primitive versions rename them: getAsInt(), applyAsDouble()…
Trap 2 — Count the generic parameters
Function<List<String>> doesn't compile: Function needs <T, R>. BiFunction needs three; the operators take one.
Trap 3 — Operators force one type
UnaryOperator<Long> u = x -> 3.14; doesn't compile: the result must be a Long.
Trap 4 — Bi adds exactly one parameter
BiConsumer, BiPredicate, BiFunction exist. There is no BiSupplier, BinaryConsumer or BinaryFunction.
Trap 5 — Predicate versus Function
"Returns a boolean" (primitive) → Predicate<T>. "Returns a Boolean" (object) → Function<T, Boolean>.
Trap 6 — andThen() versus compose()
f.andThen(g) runs f then g; f.compose(g) runs g then f. Compute both before answering.
Trap 7 — Only one interface for boolean
BooleanSupplier is the only one. No BooleanPredicate, CharSupplier, FloatSupplier or StringSupplier.
Practice questions¶
1. Which interface fills the blank?
var catalog = new HashMap<String, Integer>();
_________<String, Integer> addShow = catalog::put;
addShow.accept("Dark", 3);
A. Consumer
B. BiConsumer
C. BiFunction
D. BinaryOperator
Answer
B. accept() with two inputs is BiConsumer. Consumer takes one type parameter, BiFunction needs three and uses apply(), and BinaryOperator takes one type and uses apply().
2. What does this print?
Function<Integer, Integer> addAds = m -> m + 10;
Function<Integer, Integer> half = m -> m / 2;
Function<Integer, Integer> plan = addAds.compose(half);
System.out.println(plan.apply(60) + " " + addAds.andThen(half).apply(60));
A. 35 40
B. 40 35
C. 70 35
D. 40 40
Answer
B. compose runs half first: 60 / 2 = 30, + 10 = 40. andThen runs addAds first: 60 + 10 = 70, / 2 = 35.
3. Which two lines compile? (User has a public no-argument constructor.)
1: Supplier<User> s = User::new;
2: Predicate<String> p = String::isEmpty;
3: UnaryOperator<Integer> u = x -> x * 1.5;
4: Function<Episode> f = e -> e.title();
A. 1 and 2
B. 2 and 3
C. 1 and 4
D. 3 and 4
Answer
A. Line 3 returns a double where an Integer is required. Line 4 gives Function only one type parameter.