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

✅ Answer:

Java 8 introduced several major features:

  • Lambda Expressions

  • Functional Interfaces

  • Streams API

  • Default and Static Methods in Interfaces

  • Optional Class

  • New Date and Time API (java.time)

  • Method References

  • Collectors and the java.util.function package

  • Nashorn JavaScript Engine

🔹 2. What is a Lambda Expression?

✅ Answer:

A Lambda Expression is an anonymous function (without a name) used primarily to implement methods of functional interfaces in a concise way.

Example:

// Traditional way

Runnable r1 = new Runnable() {

    public void run() {

        System.out.println("Hello");

    }

};

 

// Lambda way

Runnable r2 = () -> System.out.println("Hello");

 

🔹 3. What is a Functional Interface?

✅ Answer:

A Functional Interface is an interface that has exactly one abstract method. It can have multiple default or static methods.

Example:

@FunctionalInterface

interface MyFunc {

    void doSomething();

}

 

  • Common functional interfaces: Runnable, Callable, Comparator, Predicate, Function, Consumer, etc.

🔹 4. What is the use of the @FunctionalInterface annotation?

✅ Answer:

  • It is used to ensure the interface has exactly one abstract method.

  • If more than one abstract method is added, the compiler will throw an error.

🔹 5. What is the Stream API?

✅ Answer:

The Stream API is used to process collections of objects in a functional style. It supports operations like filter, map, reduce, collect, and more.

Example:

List<String> names = Arrays.asList("John", "Jane", "Jack");

names.stream()

     .filter(name -> name.startsWith("J"))

     .forEach(System.out::println);

 

🔹 6. Difference between Stream and Collection?

Feature

Stream

Collection

Processing

Lazy & functional

Eager & imperative

Storage

Doesn’t store data

Stores data

Traversal

One-time use

Can be traversed multiple times

Operations

Functional (filter, map, reduce)

Structural (add, remove, etc.)

🔹 7. What are default methods in interfaces?

✅ Answer:

  • Introduced in Java 8 to allow methods in interfaces with a default implementation.

  • Helps in backward compatibility.

Example:

interface MyInterface {

    default void sayHello() {

        System.out.println("Hello from interface");

    }

}

 

🔹 8. What is the Optional class? Why is it used?

✅ Answer:

Optional<T> is a container object which may or may not contain a non-null value.

Used to:

  • Avoid null checks

  • Prevent NullPointerException

Example:

Optional<String> name = Optional.of("John");

name.ifPresent(n -> System.out.println(n));

 

🔹 9. How do you use map() and flatMap() in streams?

✅ map():

  • Transforms each element.

List<String> names = Arrays.asList("a", "b");

List<String> upper = names.stream().map(String::toUpperCase).collect(Collectors.toList());

 

✅ flatMap():

  • Flattens nested streams.

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

List<String> flat = list.stream().flatMap(Collection::stream).collect(Collectors.toList());

 

🔹 10. What are method references?

✅ Answer:

Method references are shorthand for calling methods using :: operator.

Example:

List<String> names = Arrays.asList("John", "Jane");

names.forEach(System.out::println); // method reference

 

Types:

  • Static methodClassName::staticMethod

  • Instance methodobj::instanceMethod

  • ConstructorClassName::new

🔹 11. What is the difference between Predicate and Function interfaces?

Feature

Predicate

Function

Package

java.util.function.Predicate

java.util.function.Function

Method

boolean test(T t)

R apply(T t)

Purpose

Testing a condition

Mapping input to output

🔹 12. Explain the new Date and Time API in Java 8.

✅ Answer:

Java 8 introduced the java.time package with classes:

  • LocalDate, LocalTime, LocalDateTime

  • ZonedDateTime, Instant, Duration, Period

Example:

LocalDate today = LocalDate.now();

LocalDate birthday = LocalDate.of(1990, Month.JANUARY, 1);

 

🔹 13. What is the difference between findFirst() and findAny() in streams?

Method

Behavior

findFirst()

Returns the first element in stream

findAny()

Returns any element (best effort, especially in parallel streams)

🔹 14. Can you write a simple Lambda expression for sorting a list?

✅ Answer:

List<String> names = Arrays.asList("Zoe", "John", "Alice");

names.sort((a, b) -> a.compareTo(b));