Skip to content

Part 8 practice questions: functional programming

☕ Java 17 · 🎓 OCP 1Z0-829 · Part 8 · Functional Programming

Optional page

Eleven exam-style questions covering all of Part 8. Try each one before opening its answer. To review a topic, go back to the Part 8 recap.

1. Which lambda fills the blank so the code compiles?

interface ContentFilter { boolean accept(Movie movie); }
ContentFilter newReleases = ____________________;

A. m -> { m.isNew() }
B. (Movie m) -> return m.isNew();
C. Movie m -> m.isNew()
D. (var m) -> { return m.isNew(); }

Answer

D. var in parentheses, a block with return and ;. A has braces without return or ;, B uses return without braces, C declares a type without parentheses.

2. Which of these are functional interfaces?

interface Rateable     { int rate(); String toString(); }
interface Playable     { void play(); default void pause() {} }
interface Downloadable { boolean equals(Downloadable d); void download(); }
interface Streamable extends Playable { void stream(); }

A. Rateable and Playable only
B. Playable only
C. Playable and Streamable only
D. All four

Answer

A. toString() matches Object, so Rateable has one abstract method; Playable's default doesn't count. equals(Downloadable) isn't Object's signature → two. Streamable inherits play() and adds stream() → two.

3. Which interfaces complete blanks 1, 2 and 3?

____1____<User>    greet  = u -> System.out.println("Hi " + u.name());
____2____<Movie>   isHd   = m -> m.quality() == Quality.HD;
____3____<Episode> nextEp = () -> catalog.firstEpisode();

A. Consumer, Predicate, Supplier
B. Function, Predicate, Supplier
C. Consumer, Function, Supplier
D. Supplier, Predicate, Consumer

Answer

A. A User in, nothing out → Consumer. A Movie in, a boolean out → Predicate. Nothing in, an Episode out → Supplier.

4. Which lambda is equivalent to String::startsWith here?

BiPredicate<String, String> match = String::startsWith;
System.out.println(match.test("Stranger Things", "Str"));   // true

A. (t, p) -> t.startsWith(p)
B. (t, p) -> p.startsWith(t)
C. t -> t.startsWith(p)
D. (t, p) -> String.startsWith(t, p)

Answer

A. The first parameter is the object the method runs on, the second is the argument. B swaps them, C has one parameter, D calls a static method that doesn't exist.

5. How many of these lines compile?

1: Supplier<ArrayList<Movie>> watchlist = ArrayList::new;
2: Function<String, Integer> titleLength = String::length;
3: UnaryOperator<String> shout = String::toUpperCase;
4: BiFunction<String, String> joinTitles = String::concat;

A. One
B. Two
C. Three
D. Four

Answer

C. Lines 1, 2 and 3. Line 4 fails: BiFunction needs three type parameters, two inputs and a result.

6. What does this print?

Function<Integer, Integer> addBonus = minutes -> minutes + 10;
Function<Integer, Integer> doubleIt = minutes -> minutes * 2;
var combined = doubleIt.compose(addBonus);
System.out.println(combined.apply(5));

A. 20
B. 30
C. 25
D. The code does not compile

Answer

B. compose runs its argument first: 5 + 10 = 15, then 15 × 2 = 30. andThen would give 20.

7. Given record Movie(String title, int year, String rating), what is printed?

Predicate<Movie> isNew  = m -> m.year() >= 2024;
Predicate<Movie> isKids = m -> m.rating().equals("G");
var forAdults = isNew.and(isKids.negate());
System.out.println(forAdults.test(new Movie("Toy Box", 2025, "G")));

A. true
B. false
C. The code does not compile
D. An exception is thrown

Answer

B. isNew is true; isKids is true, so its negation is false. true AND false → false.

8. Which types can fill the blank? (Choose all that apply.)

double price = 15.99;
______________ toCents = p -> (int) (p * 100);
System.out.println(toCents.applyAsInt(price));   // 1599

A. DoubleToIntFunction
B. DoubleFunction<Integer>
C. ToIntFunction<Double>
D. IntFunction<Double>

Answer

A and C. Both have applyAsInt(): A takes a double, C a Double (the price is autoboxed). B and D use apply(), and D expects an int.

9. Which parameter list makes this line compile?

BiPredicate<User, Subscription> canWatch = ____________________;

A. (var u, Subscription s) -> true
B. (final var u, var s) -> true
C. (var u, s) -> true
D. (User u, s) -> true

Answer

B. var for both, and final is allowed with var. A, C and D mix styles.

10. Which lines cause a compiler error?

1: public void announce(List<User> users) {
2:     String message = "New season available!";
3:     int sent = 0;
4:     users.forEach(u -> u.send(message));
5:     users.forEach(u -> sent++);
6:     message = "Watch it now!";
7: }

A. Line 4 only
B. Line 5 only
C. Lines 4 and 5
D. Line 6 only

Answer

C. Line 6 reassigns message, so line 4 can't use it. Line 5 modifies sent inside a lambda. Line 6 itself is legal: errors are reported where the lambda uses the variable.

11. How many lines contain an error?

1: public void rate(Movie movie) {
2:     int score = 5;
3:     Predicate<Movie> worthIt = movie -> {
4:         int score = 8;
5:         return movie.stars() >= score; }
6: }

A. Zero
B. One
C. Two
D. Three

Answer

D. Line 3 reuses movie, already a parameter. Line 4 redeclares score. Line 5 is missing the ; after the } that closes the lambda statement.

← Part 8 recap · All 1Z0-829 pages