Lambda Expressions and Functional Interfaces practice questions

From OCA Java SE 8 (1Z0-808) · 13 questions on this topic

Lambda Expressions and Functional Interfaces practice questions from OCA Java SE 8 (1Z0-808). This pack has 13 questions tagged Lambda Expressions and Functional Interfaces, drawn from its timed mock exams. 8 of them are worked through in full below — the question, every option, why each is right or wrong, and the explanation.

Worked examples for Lambda Expressions and Functional Interfaces

  1. Question 1

    What is the output of the following program? ```java import java.util.function.Predicate; public class Main { static String check(int n, Predicate<Integer> p) { return p.test(n) ? "pass" : "fail"; } public static void main(String[] args) { System.out.println(check(4, n -> n % 2 == 0)); } } ```

    1. A. fail

      "fail" is the ternary's branch for a false predicate, but 4 % 2 == 0 is true, so the method returns "pass" instead.

    2. B. passCorrect answer

      The lambda supplies the Predicate target type for the parameter, and 4 % 2 == 0 is true, so the ternary maps the boolean to "pass".

    3. C. Compilation fails because a lambda cannot be a method argument

      Passing a lambda where a Predicate parameter is expected is the standard target-typing pattern; the parameter supplies the type, so this compiles rather than failing.

    4. D. true

      The method never prints the raw boolean; its ternary converts the predicate result into the word "pass" or "fail", so "true" is never output.

    Explanation

    Passing a lambda where a Predicate parameter is expected is the standard pattern (ruling out `Compilation fails because a lambda cannot be a method...`) — the parameter supplies the target type. 4 % 2 == 0 is true, so check returns "pass" (the ternary maps the boolean to a word — `true` prints the raw boolean, which never happens here).

  2. Question 2

    What makes an interface usable as the target type of a lambda expression?

    1. A. It is annotated with @FunctionalInterface

      The annotation is optional documentation that merely makes the compiler enforce the single-abstract-method rule; an interface is usable as a lambda target with or without it.

    2. B. It declares no methods at all

      An interface with zero abstract methods gives the lambda nothing to implement; a functional interface needs exactly one abstract method, not none.

    3. C. It extends java.util.function.Predicate

      This wrongly restricts lambda targets to the built-in java.util.function types; any interface with exactly one abstract method works, not just Predicate or its subtypes.

    4. D. It declares exactly one abstract methodCorrect answer

      A functional interface is defined by having exactly one abstract method — that single method is what the lambda implements (JLS 8 §9.8).

    Explanation

    A functional interface is defined by having exactly ONE abstract method — that's what the lambda implements. The @FunctionalInterface annotation is optional documentation that makes the compiler enforce the rule (`It is annotated with @FunctionalInterface`), and any such interface works, not just the java.util.function ones (`It extends java.util.function.Predicate`).

  3. Question 3

    What is the result of compiling the following program? ```java import java.util.function.Predicate; public class Main { public static void main(String[] args) { Predicate<String> p = String s -> s.isEmpty(); System.out.println(p.test("")); } } ```

    1. A. true

      The program never runs: declaring the parameter type without parentheses is a syntax error, so no boolean is ever printed.

    2. B. false

      There is no runtime output at all because the malformed lambda syntax prevents the program from compiling.

    3. C. Compilation failsCorrect answer

      Once the parameter type is declared, parentheses are mandatory: `(String s) -> ...`. A bare `String s -> ...` is a syntax error, so the program does not compile.

    4. D. It compiles with a warning

      The missing parentheses around the typed parameter is a hard syntax error, not a warning; the compiler rejects the code outright rather than accepting it with a diagnostic.

    Explanation

    Once you DECLARE the parameter's type, parentheses become mandatory: `(String s) -> ...`. A bare `String s -> ...` is a syntax error. Only a single UNTYPED parameter may omit the parentheses.

  4. Question 4

    What is the output of the following program? ```java import java.util.function.Predicate; public class Main { public static void main(String[] args) { Predicate<String> empty = s -> s.isEmpty(); System.out.println(empty.test("") + " " + empty.test("x")); } } ```

    1. A. true falseCorrect answer

      test("") returns true because the empty string is empty, and test("x") returns false because "x" is not, printed in that order with a space between.

    2. B. false true

      This swaps the two results: it would require the empty string to be non-empty and "x" to be empty, which is the reverse of what isEmpty reports.

    3. C. Compilation fails because the lambda parameter has no type

      This misunderstands type inference: the parameter type is inferred from the Predicate<String> target type, so an explicit declaration is not required and the code compiles.

    4. D. An exception is thrown at runtime

      Calling isEmpty on the non-null literal strings never throws; the program runs to completion and simply prints the two boolean results.

    Explanation

    The lambda implements Predicate<String>'s single abstract method test(T). The parameter type is INFERRED from the target type, so no declaration is needed (ruling out `Compilation fails because the lambda parameter...`). "" is empty (true), "x" is not (false).

  5. Question 5

    What is the result of compiling the following program? ```java import java.util.function.Predicate; public class Main { public static void main(String[] args) { Predicate<String> p = () -> true; System.out.println(p.test("x")); } } ```

    1. A. Compilation failsCorrect answer

      Predicate.test takes ONE argument, but () -> true declares zero parameters, so the lambda's shape does not match the functional interface: "incompatible parameter types" (JLS 8 §15.27.3).

    2. B. true

      The body would yield true, but the zero-parameter shape never matches Predicate.test, so nothing runs.

    3. C. false

      The mismatch is in the parameter count, not the returned value — and the code never runs.

    4. D. An exception is thrown at runtime

      The arity is checked at compile time, so there is no runtime in which to throw.

    Explanation

    The lambda's shape must match the functional interface's abstract method: Predicate.test takes ONE argument, but () -> true declares zero parameters — "incompatible parameter types". The arity is checked at compile time.

  6. Question 6

    What is the result of compiling the following program? ```java import java.util.function.Predicate; public class Main { public static void main(String[] args) { int limit = 5; Predicate<Integer> p = n -> n > limit; limit = 6; System.out.println(p.test(10)); } } ```

    1. A. true

      No value is printed because the lambda captures `limit`, which is reassigned later and therefore not effectively final, causing a compile error before test can run.

    2. B. false

      The program yields no runtime output; reassigning the captured `limit` variable breaks the effectively-final requirement, so the code fails to compile.

    3. C. Compilation failsCorrect answer

      A lambda may only capture local variables that are effectively final. The later `limit = 6;` reassignment poisons the capture, so the compiler rejects the lambda's use of `limit`.

    4. D. An exception is thrown at runtime

      The effectively-final violation is caught at compile time, not runtime; the program never executes, so no exception is thrown.

    Explanation

    A lambda may only capture local variables that are EFFECTIVELY FINAL — assigned exactly once. The later `limit = 6;` breaks that, and the compiler rejects the lambda's use of limit ("local variables referenced from a lambda expression must be final or effectively final"). Note the reassignment AFTER the lambda still poisons it.

  7. Question 7

    What is the output of the following program? ```java import java.util.function.Predicate; public class Main { public static void main(String[] args) { Predicate<String> p = s -> { return s.length() > 2; }; System.out.println(p.test("ab")); } } ```

    1. A. true

      This would require the length test to succeed, but "ab".length() is 2 and 2 > 2 is false — the comparison is strictly greater-than, not greater-or-equal.

    2. B. falseCorrect answer

      "ab".length() is 2, and 2 > 2 is false, so the block-body lambda returns false and test("ab") prints it.

    3. C. 2

      This confuses the string's length with the predicate's result; test returns the boolean of the comparison itself, never the length value.

    4. D. Compilation fails

      The block-body form with braces and an explicit return is a valid lambda, so the code compiles and runs normally.

    Explanation

    "ab".length() is 2, and 2 > 2 is false. The block-body form with return is valid (so `Compilation fails` is wrong), and test returns the boolean itself, not the length (`2`).

  8. Question 8

    What is the result of compiling the following program? ```java import java.util.function.Predicate; public class Main { public static void main(String[] args) { Predicate<Integer> p = n -> return n > 0; System.out.println(p.test(5)); } } ```

    1. A. true

      No value is printed because mixing an expression body with the `return` keyword is a syntax error that stops the program from compiling.

    2. B. false

      The code produces no runtime output; the `return` statement placed in an unbraced expression body is a syntax error, so nothing runs.

    3. C. An exception is thrown at runtime

      This is a compile-time syntax error, not a runtime problem; the program never reaches execution, so no exception can be thrown.

    4. D. Compilation failsCorrect answer

      `return` is a statement and may only appear inside a braced block body (`n -> { return n > 0; }`); an expression body must be just the expression (`n -> n > 0`), so mixing the two forms fails to compile.

    Explanation

    return is a STATEMENT, so it may only appear inside a block body with braces: `n -> { return n > 0; }`. An expression body must be just the expression: `n -> n > 0`. Mixing the two forms is a syntax error.

Practise all 13 Lambda Expressions and Functional Interfaces questions

OCA Java SE 8 has the full set, inside timed mock exams that mirror real exam conditions — every question with a worked explanation.

Open OCA Java SE 8

Other topics in this pack