Java Lambdas & Functional Programming
Master lambda expressions, method references, built-in functional interfaces, Optional, and functional programming patterns in Java.
Questions in this quiz
20 questions on Java Lambdas & Functional Programming. Work through them here, then start the quiz to answer them and see your score.
1.What is a lambda expression in Java?
- AA named method that can be stored in a variable
- BA compile-time constant expression
- CA concise anonymous function that can be passed as an argument or assigned to a functional interface
- DA special type of class with no fields
2.What is the syntax of a lambda expression?
- A(parameters) -> body
- Blambda(parameters): body
- C[parameters] { body }
- Dfunction(params) => body
3.What does this code print?
import java.util.*; public class Main { public static void main(String[] args) { List<String> names = Arrays.asList("Charlie", "Alice", "Bob"); names.sort((a, b) -> a.compareTo(b)); names.forEach(System.out::println); } }- ACharlie Alice Bob
- BBob Alice Charlie
- CAlice Bob Charlie
- DCompilation error
4.What is a method reference in Java?
- AA shorthand for a lambda that simply calls an existing method, using the :: operator
- BA pointer to a static method only
- CA reflection-based way to invoke private methods
- DA reference to a method stored in a variable at compile time
5.What does this code print?
import java.util.*; import java.util.stream.*; public class Main { static int doubleIt(int n) { return n * 2; } public static void main(String[] args) { List<Integer> nums = Arrays.asList(1, 2, 3, 4, 5); List<Integer> doubled = nums.stream() .map(Main::doubleIt) .collect(Collectors.toList()); System.out.println(doubled); } }- ACompilation error
- B[2, 4, 6, 8, 10]
- C[1, 2, 3, 4, 5]
- D[1, 4, 9, 16, 25]
6.What is the Predicate<T> functional interface?
- AA consumer that accepts two arguments
- BA supplier that produces values of type T
- CA function that takes T and returns a boolean — used for filtering and testing
- DA function that takes T and returns another type
7.What does this code print?
import java.util.function.*; public class Main { public static void main(String[] args) { Predicate<String> isLong = s -> s.length() > 5; Predicate<String> startsWithA = s -> s.startsWith("A"); Predicate<String> both = isLong.and(startsWithA); System.out.println(both.test("Algorithm")); System.out.println(both.test("Art")); System.out.println(both.test("Bubble")); } }- Atrue false true
- Btrue true false
- Cfalse true false
- Dtrue false false
8.What is the Function<T, R> interface?
- AA function that takes two arguments
- BA function that takes no arguments and returns R
- CA function that takes T and returns R — used for transformations
- DA function that takes T and returns void
9.What does this code print?
import java.util.function.*; public class Main { public static void main(String[] args) { Function<Integer, Integer> times3 = n -> n * 3; Function<Integer, Integer> plus10 = n -> n + 10; Function<Integer, Integer> combined = times3.andThen(plus10); System.out.println(combined.apply(5)); } }- A45
- B35
- C65
- D25
10.What are Consumer<T> and Supplier<T>?
- ABoth take T and return boolean
- BConsumer is for reading; Supplier is for writing
- CConsumer takes T and returns void (side-effect); Supplier takes nothing and returns T (factory)
- DConsumer takes T and returns T; Supplier takes nothing and returns nothing
11.What does this code print?
import java.util.function.*; public class Main { public static void main(String[] args) { Supplier<String> greeting = () -> "Hello, World!"; Consumer<String> printer = s -> System.out.println(s.toUpperCase()); printer.accept(greeting.get()); } }- AHELLO, WORLD!
- Bhello, world!
- CCompilation error
- DHello, World!
12.What is Optional<T> and why was it introduced?
- AAn annotation for optional method parameters
- BA collection that holds at most one element
- CA thread-safe wrapper for nullable values
- DA container that may or may not hold a value — introduced to reduce NullPointerExceptions and make absence explicit
13.What does this code print?
import java.util.*; public class Main { public static void main(String[] args) { Optional<String> opt1 = Optional.of("hello"); Optional<String> opt2 = Optional.empty(); System.out.println(opt1.map(String::toUpperCase).orElse("none")); System.out.println(opt2.map(String::toUpperCase).orElse("none")); } }- Anone none
- Bhello none
- CHELLO HELLO
- DHELLO none
14.What is the difference between orElse() and orElseGet()?
- AorElse() is for primitives; orElseGet() is for objects
- BorElseGet() throws an exception; orElse() does not
- CThey are identical in behaviour
- DorElse() always evaluates the default expression; orElseGet() lazily evaluates via a Supplier only when the value is absent
15.What does this code print?
import java.util.*; public class Main { public static void main(String[] args) { Optional<Integer> opt = Optional.of(42) .filter(n -> n > 50) .map(n -> n * 2); System.out.println(opt.isPresent()); System.out.println(opt.orElse(-1)); } }- Afalse -1
- Btrue 42
- Cfalse 42
- Dtrue 84
16.What is a closure in the context of Java lambdas?
- AA lambda that closes the program when called
- BA lambda with no parameters
- CA lambda that captures variables from the enclosing scope — those variables must be effectively final
- DA private inner class used instead of a lambda
17.What does this code cause?
import java.util.function.*; public class Main { public static void main(String[] args) { int x = 10; Supplier<Integer> s = () -> x * 2; // x = 20; // uncommenting this causes an error System.out.println(s.get()); } }- APrints 20 — lambdas capture by reference
- BCompilation error if x = 20 is uncommented — x must be effectively final
- CPrints 20 — x is effectively final so the lambda captures it
- DPrints 20 — x is modified before get() is called
18.What is BiFunction<T, U, R>?
- AA predicate that tests two conditions
- BA function that runs twice
- CA function that takes two arguments of types T and U, and returns a result of type R
- DA function that takes two identical arguments
19.What does this code print?
import java.util.function.*; public class Main { public static void main(String[] args) { BiFunction<String, Integer, String> repeat = (s, n) -> s.repeat(n); UnaryOperator<String> exclaim = s -> s + "!"; Function<String, String> combined = repeat.andThen(exclaim); System.out.println(combined.apply("hi", 3)); } }- Ahi!hi!hi!
- Bhihihi!
- Chi!hi!hi
- DCompilation error
20.What is the key benefit of writing code in a functional style with lambdas?
- ADeclarative style, composability, and easier reasoning about code with fewer side effects
- BFunctional code cannot have bugs
- CFunctional code always runs faster than imperative code
- DLambdas use less memory than anonymous classes