Lambdas & Functional Interfaces Questions with Answers

22 practice questions on Lambdas & Functional Interfaces (Core Java) with answers and step-by-step solutions. Try 10 samples below, then practise all 22 free in the AptiNest app.

Sample questions

Question 1Easy

Which of the following is the correct lambda for Comparator<String> that orders strings by length?

  1. a -> a.length()
  2. (a, b) -> a.length() - b.length()
  3. () -> a.length() - b.length()
  4. (a, b) -> { a.length() < b.length(); }

Answer: B. (a, b) -> a.length() - b.length()

Explanation

Comparator.compare takes two arguments and returns an int; a negative result means a comes first.

Step-by-step solution

  1. Comparator<String> needs compare(a, b) that returns an int.
  2. a.length() minus b.length() is negative when a is shorter, so a sorts first.
  3. Option B is the only lambda with two parameters and an int result.

Why the other options are wrong

  • A. A one-parameter lambda cannot implement compare, which needs two strings.
  • C. A zero-parameter lambda cannot implement compare, which needs two arguments.
  • D. A block with no return statement does not return an int, so it does not compile.

Tip: For a Comparator, subtract the keys: a minus b gives ascending order.

Question 2Easy

Which method-reference form refers to an instance method of an arbitrary object of a particular type?

  1. Math::abs
  2. String::toUpperCase
  3. System.out::println
  4. ArrayList::new

Answer: B. String::toUpperCase

Explanation

String::toUpperCase is unbound: the first lambda argument becomes the receiver. System.out::println is bound to a specific object, Math::abs is static and ArrayList::new is a constructor reference.

Step-by-step solution

  1. String::toUpperCase refers to an instance method of any String object.
  2. The method is called on the first argument passed to the lambda expression.
  3. This makes it a reference to an unbound instance method of a particular type.
  4. Other options are static methods, constructor references, or bound methods.
  5. Thus, String::toUpperCase is the correct answer.

Why the other options are wrong

  • A. Math::abs is a static method reference, not an instance method on an arbitrary object.
  • C. System.out::println is a bound instance method on one particular object, System.out.
  • D. ArrayList::new is a constructor reference, which creates new objects rather than calling an instance method.

Tip: Remember: Type::methodName refers to an unbound instance method.

Question 3Easy

What is the output?

import java.util.*;
import java.util.function.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) {
        Function<Integer, Integer> sq = x -> x * x;
        Function<Integer, Integer> inc = x -> x + 1;
        System.out.println(sq.andThen(inc).apply(3) + " " + sq.compose(inc).apply(3));
    }
}
  1. 10 16
  2. 16 16
  3. 16 10
  4. 10 10

Answer: A. 10 16

Explanation

andThen applies sq first then inc: 9 + 1 = 10.
compose applies inc first then sq: (3 + 1)^2 = 16.

Step-by-step solution

  1. andThen(inc) applies sq first: 3 squared is 9, then inc gives 10.
  2. compose(inc) applies inc first: 3 plus 1 is 4, then sq gives 16.
  3. The printed result is 10 16, which is option A.

Why the other options are wrong

  • B. AndThen applies sq then inc, giving 10, not 16.
  • C. Compose applies inc first and sq second, giving 16, not 10.
  • D. Compose gives 16, not 10, so the second value is wrong.

Tip: AndThen runs the first function then the second; compose runs them in the reverse order.

Question 4Easy

What is the output?

import java.util.*;
import java.util.function.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) {
        Optional<String> o = Optional.ofNullable(null);
        System.out.println(o.isPresent() + " " + o.orElse("none"));
    }
}
  1. false none
  2. NullPointerException is thrown
  3. true null
  4. false null

Answer: A. false none

Explanation

ofNullable(null) returns an empty Optional; Optional.of(null) would throw NullPointerException instead.

Step-by-step solution

  1. Optional.ofNullable(null) creates an empty Optional object.
  2. o.isPresent() checks if the Optional contains a value, which is false.
  3. o.orElse("none") returns "none" because the value is absent.
  4. So the output is false none.

Why the other options are wrong

  • B. Creating it with ofNullable(null) gives an empty Optional, so no NPE happens and orElse returns none.
  • C. isPresent is false for an empty Optional, and orElse returns the text none, not null.
  • D. orElse with none returns the default text none when the Optional is empty, not null.

Tip: Remember: ofNullable(null) gives empty Optional, so isPresent() is false and orElse() uses default.

Question 5Medium

What is the output?

import java.util.*;
import java.util.function.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) {
        Optional<String> name = Optional.of("java");
        System.out.println(name.map(String::length).filter(n -> n > 5).orElse(-1));
    }
}
  1. Optional.empty
  2. -1
  3. 4
  4. java

Answer: B. -1

Explanation

map gives Optional[4]; filter(n > 5) fails, producing an empty Optional, so orElse returns -1.

Step-by-step solution

  1. map(String::length) gives Optional[4].
  2. filter(n > 5) fails.
  3. The Optional is empty, so orElse returns -1.

Why the other options are wrong

  • A. orElse returns a value, not an Optional.
  • C. 4 was filtered out.
  • D. map changed it to a length.

Tip: orElse gives the fallback.

Question 6Medium

What is the output?

import java.util.*;
import java.util.function.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) {
        List<String> names = new ArrayList<>(List.of("Ravi", "asha", "Meera"));
        names.sort(String::compareToIgnoreCase);
        System.out.println(names);
    }
}
  1. [asha, Ravi, Meera]
  2. [Meera, Ravi, asha]
  3. [Ravi, asha, Meera]
  4. [asha, Meera, Ravi]

Answer: D. [asha, Meera, Ravi]

Explanation

String::compareToIgnoreCase is an unbound method reference acting as Comparator<String>; ignoring case gives asha < Meera < Ravi. Plain compareTo would put uppercase first.

Step-by-step solution

  1. CompareToIgnoreCase compares the names ignoring case: asha, then Meera, then Ravi.
  2. Plain compareTo would put uppercase names first, but that is not used here.
  3. The sorted result is asha, Meera, Ravi, which is option D.

Why the other options are wrong

  • A. Ravi comes after Meera when case is ignored, so Ravi is not second.
  • B. Asha sorts first when case is ignored, not last.
  • C. Ravi is last when case is ignored, not second.

Tip: Case-insensitive sorting compares letters as if they were all lower case.

Question 7Medium

What is the output?

import java.util.*;
import java.util.function.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) {
        int base = 10;
        Function<Integer, Integer> add = x -> x + base;
        base = 20;
        System.out.println(add.apply(1));
    }
}
  1. Compilation error
  2. 1
  3. 11
  4. 21

Answer: A. Compilation error

Explanation

A local variable captured by a lambda must be final or effectively final; reassigning base makes the capture illegal.

Step-by-step solution

  1. The lambda captures the variable base.
  2. The value of base is 10 when the lambda is created.
  3. Then base is reassigned to 20, which makes it not effectively final.
  4. This causes a compilation error because the captured variable must be final or effectively final.
  5. Final answer: Compilation error.

Why the other options are wrong

  • B. The code does not compile, so no value is printed; base is not effectively final.
  • C. The code does not compile, so 11 would need base to stay 10, which the reassignment breaks.
  • D. The code does not compile at all, so no value of 21 is ever printed.

Tip: Remember: Lambdas can only capture final or effectively final variables.

Question 8Medium

What is the output?

import java.util.*;
import java.util.function.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) {
        Optional<String> o = Optional.empty();
        System.out.println(o.get());
    }
}
  1. null
  2. NullPointerException is thrown
  3. Optional.empty
  4. NoSuchElementException is thrown

Answer: D. NoSuchElementException is thrown

Explanation

Calling get() on an empty Optional throws NoSuchElementException; use orElse, orElseGet or orElseThrow to handle absence explicitly.

Step-by-step solution

  1. Optional.empty() creates an empty optional object.
  2. Calling get() on an empty optional throws NoSuchElementException.
  3. The code attempts to print the result of o.get(), which is not present.
  4. Thus, a NoSuchElementException is thrown at runtime.
  5. The correct answer is NoSuchElementException is thrown.

Why the other options are wrong

  • A. get() on an empty Optional throws an exception rather than returning null.
  • B. Calling get() throws NoSuchElementException on an empty Optional, not NullPointerException.
  • C. println calls get() first, which throws; it never prints the Optional.empty text.

Tip: Use orElse to avoid exceptions when accessing Optional values.

Question 9Hard

What is the output?

import java.util.*;
import java.util.function.*;
import java.util.stream.*;

@FunctionalInterface
interface Calc {
    int calc(int a, int b);
    default int twice(int a, int b) { return 2 * calc(a, b); }
}

public class Main {
    public static void main(String[] args) {
        Calc add = (a, b) -> a + b;
        System.out.println(add.calc(2, 3) + " " + add.twice(2, 3));
    }
}
  1. 5 10
  2. 5 5
  3. 10 10
  4. Compilation error

Answer: A. 5 10

Explanation

A default method does not break the single-abstract-method rule, so the lambda implements calc and twice() reuses it.

Step-by-step solution

  1. The lambda (a, b) -> a + b implements the single abstract method calc.
  2. add.calc(2, 3) returns 5.
  3. add.twice(2, 3) returns 2 times calc, which is 10.
  4. The output is 5 10, which is option A.

Why the other options are wrong

  • B. Twice returns 2 times 5, which is 10, not 5.
  • C. calc(2, 3) is 5, not 10.
  • D. A default method is allowed alongside one abstract method, so it compiles.

Tip: A default method adds behaviour to a functional interface without breaking the lambda.

Question 10Hard

What is the output?

import java.util.*;
import java.util.function.*;
import java.util.stream.*;

public class Main {
    String name = "outer";

    void go() {
        Runnable r1 = () -> System.out.print(this.name + " ");
        Runnable r2 = new Runnable() {
            String name = "inner";
            public void run() { System.out.println(this.name); }
        };
        r1.run();
        r2.run();
    }

    public static void main(String[] args) {
        new Main().go();
    }
}
  1. outer inner
  2. outer outer
  3. inner outer
  4. inner inner

Answer: A. outer inner

Explanation

Inside a lambda, this refers to the enclosing instance; inside an anonymous class, this refers to the anonymous object itself.

Step-by-step solution

  1. R1 is a lambda, so this.name refers to the outer Main object's field, which is outer.
  2. R1 prints outer followed by a space using print.
  3. R2 is an anonymous class, so this.name is its own field inner, printed with println.
  4. The output is outer inner, which is option A.

Why the other options are wrong

  • B. The anonymous class has its own name field, so it prints inner, not outer.
  • C. The lambda uses the enclosing instance, so it prints outer, not inner.
  • D. Only the anonymous class sees inner; the lambda prints outer.

Tip: Inside a lambda this is the enclosing object; inside an anonymous class it is the anonymous object.

Practise all 22 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Core Java topics · All topics