Skip to content
C

Java Interview Questions

Java 8+ Interview Questions

Lambdas, functional interfaces, the Stream API, Optional, and default/static interface methods.

Question 1: What is a lambda expression, and what problem does it solve?

Ans

A lambda expression is a compact, inline way to write the implementation of a functional interface, letting you pass behavior as a value without writing a full named class. It solves the verbosity problem of anonymous inner classes, which needed several lines of boilerplate just to implement a single method.

Example

java
List<String> names = List.of("Amit", "Neha"); names.forEach(name -> System.out.println(name));

Important Point

A lambda's parameter types are usually inferred from the functional interface's abstract method, so you rarely need to write them explicitly.

Question 2: What is a functional interface, and how does it relate to lambda expressions?

Ans

A functional interface is an interface with exactly one abstract method, though it may still have default or static methods, and it's the type that a lambda expression or method reference actually implements. The compiler checks that your lambda's parameter list and return type match that single abstract method.

Example

java
@FunctionalInterface interface Calculator { int add(int a, int b); } Calculator c = (a, b) -> a + b;

Important Point

The @FunctionalInterface annotation is optional but recommended — it makes the compiler flag an error if a second abstract method is accidentally added later.

Question 3: What is a method reference, and what are its common forms?

Ans

A method reference is a shorthand for a lambda that would only call one existing method, referring to that method directly by name instead of writing out the lambda body. Common forms include a static method reference, an instance method reference on a particular object, an instance method reference on an arbitrary object of a type, and a constructor reference.

Example

java
names.forEach(System.out::println); // instance method on a particular object list.stream().map(String::toUpperCase); // instance method on an arbitrary object Supplier<ArrayList<String>> factory = ArrayList::new; // constructor reference

Important Point

A method reference must still be assignment-compatible with the target functional interface's parameter and return types, exactly like a lambda would need to be.

Question 4: What is the Stream API, and how is a Stream different from a Collection?

Ans

The Stream API provides a declarative way to process sequences of data through a pipeline of operations like filter, map, and collect, describing what should happen to the data rather than manually writing loops. A Stream isn't a data structure that stores elements the way a Collection does — it represents a computation over a source of data, is typically consumed once, and can be lazily evaluated, only doing work when a terminal operation is invoked.

Example

java
List<Integer> result = numbers.stream() .filter(n -> n % 2 == 0) .map(n -> n * 2) .toList();

Important Point

Trying to reuse a Stream after a terminal operation has already consumed it throws IllegalStateException.

Question 5: What is the difference between filter(), map(), and flatMap()?

Ans

filter() keeps only the elements that satisfy a condition, without changing their type. map() transforms each element into exactly one new value, which may be a different type. flatMap() is used when each element itself produces a stream of values, like a list of lists, mapping each element to a stream and then flattening all those streams into a single combined stream.

Example

java
List<List<Integer>> nested = List.of(List.of(1, 2), List.of(3, 4)); List<Integer> flat = nested.stream() .flatMap(List::stream) // flattens List<List<Integer>> into a single stream .toList(); // [1, 2, 3, 4]

Important Point

If you use map() where you needed flatMap(), you end up with a stream of streams instead of a single flat stream of values.

Question 6: What is Optional, and why was it introduced?

Ans

Optional<T> is a container that explicitly represents either a present value or an absent one, making the possibility of "no result" visible in a method's return type instead of silently returning null and risking a NullPointerException wherever the caller forgets to check.

Example

java
Optional<String> name = Optional.ofNullable(getName()); name.ifPresent(System.out::println); String value = name.orElse("Unknown");

Important Point

Optional is intended mainly as a return type for methods where "no result" is a normal, expected outcome — it's generally discouraged as a field type or method parameter type.

Question 7: What are default and static methods in interfaces, and what problem did they solve in Java 8?

Ans

A default method provides a body directly inside an interface, letting existing interfaces gain new behavior without breaking every class that already implements them, since those classes automatically inherit the default implementation instead of being forced to implement the new method. Static methods on an interface belong to the interface itself, similar to utility methods, and are called through the interface name rather than through an implementing object.

Example

java
interface Vehicle { default void start() { System.out.println("Starting"); } static Vehicle basic() { return new Car(); } }

Important Point

If a class implements two interfaces with conflicting default methods of the same signature, the compiler forces the class to override the method itself and resolve the conflict explicitly.

Question 8: What is functional programming style in Java?

Ans

Java supports a functional style using lambdas, functional interfaces, method references, streams, and operations that avoid unnecessary shared mutable state.

It is particularly useful for collection transformations and declarative pipelines.

Example

java
int sum = numbers.stream().mapToInt(Integer::intValue).sum();

Important Point

Java remains a multi-paradigm language; object-oriented design is still fundamental.

Continue Your Preparation