Java 8 Interview Questions and Answers

1. What are the main features introduced in Java 8?

Answer:

  • Lambda Expressions – Enable functional programming by writing concise code.
  • Functional Interfaces – Interfaces with a single abstract method (e.g., Runnable, Comparator).
  • Streams API – Process collections in a functional style (map, filter, reduce).
  • Default and Static Methods in Interfaces – Allow method implementation inside interfaces.
  • Optional Class – Handle null values more gracefully.
  • New Date and Time API – Immutable and thread-safe classes (LocalDate, LocalTime, LocalDateTime).

2. Write a Java 8 program to filter even numbers from a list.

Answer:

java

import java.util.Arrays;
import java.util.List;

public class EvenFilter {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);

        numbers.stream()
               .filter(n -> n % 2 == 0)
               .forEach(System.out::println);
    }
}

Output:

Code

2
4
6

3. What is the difference between map() and flatMap() in Streams?

Answer:

  • map(): Transforms each element into another object. Example: Convert a list of strings to their lengths.
  • flatMap(): Flattens nested structures. Example: Convert a list of lists into a single stream of elements.

java

List<List<String>> list = Arrays.asList(
    Arrays.asList("a", "b"),
    Arrays.asList("c", "d")
);

list.stream()
    .flatMap(l -> l.stream())
    .forEach(System.out::println);

Output:

Code

a
b
c
d

4. Explain Optional in Java 8 with an example.

Answer: Optional is a container object used to avoid NullPointerException.

java

import java.util.Optional;

public class OptionalDemo {
    public static void main(String[] args) {
        Optional<String> name = Optional.ofNullable(null);

        System.out.println(name.orElse("Default Name"));
    }
}

Output:

Code

Default Name

5. How do you use reduce() in Streams?

Answer: reduce() is used to combine elements into a single result.

java

import java.util.Arrays;
import java.util.List;

public class ReduceDemo {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);

        int sum = numbers.stream()
                         .reduce(0, (a, b) -> a + b);

        System.out.println("Sum: " + sum);
    }
}

Output:

Code

Sum: 15

6. What is the difference between forEach() and forEachOrdered()?

Answer:

  • forEach(): May process elements in any order (especially in parallel streams).
  • forEachOrdered(): Guarantees processing in encounter order.

7. Write a Java 8 program to sort a list of strings in reverse order.

Answer:

java

import java.util.Arrays;
import java.util.List;

public class SortDemo {
    public static void main(String[] args) {
        List<String> names = Arrays.asList("John", "Alice", "Bob");

        names.stream()
             .sorted((a, b) -> b.compareTo(a))
             .forEach(System.out::println);
    }
}

Output:

Code

John
Bob
Alice

8. What are default methods in interfaces? Give an example.

Answer: Default methods allow interfaces to provide method implementations.

java

interface Vehicle {
    default void start() {
        System.out.println("Vehicle is starting...");
    }
}

class Car implements Vehicle {}

public class DefaultMethodDemo {
    public static void main(String[] args) {
        Car car = new Car();
        car.start();
    }
}

Output:

Code

Vehicle is starting...

Leave a Comment