top of page

Java 8 Interview Questions and Answers – Part 1 (Core Features)

Updated: 1 day ago

Java 8 is one of the most significant releases in the history of Java. It introduced functional programming concepts, Streams API, new Date-Time API, Optional, default methods, and more.

If you are preparing for interviews, you can expect Java 8 questions in almost every round, especially for mid-to-senior developers.

In this post, we’ll cover the first 10 important Java 8 interview questions with detailed explanations and code examples.

ree

Q1. What are the new features introduced in Java 8?

Java 8 introduced several groundbreaking features that changed how developers write code in Java.

Key Features:

  • Lambda Expressions → enable functional programming with concise syntax.

  • Functional Interfaces → single abstract method interfaces for Lambdas.

  • Streams API → process collections in a functional style.

  • Default & Static Methods in Interfaces → backward compatibility.

  • Optional Class → avoid NullPointerException.

  • New Date & Time API → better handling of dates and times.

  • CompletableFuture → better concurrency programming.

  • Nashorn JavaScript Engine → run JavaScript code inside Java (deprecated later).


Interview Tip: Expect the interviewer to ask for real-world use cases of these features rather than just definitions.


Q2. Explain Lambda Expressions with examples.

A Lambda expression is an anonymous function (a function without a name) that can be treated as a value. It allows us to write cleaner and more readable code.

Syntax

(parameters) -> expression
(parameters) -> { statements }

Example

// Without Lambda
Runnable r1 = new Runnable() {
    public void run() {
        System.out.println("Hello from Runnable!");
    }
};

// With Lambda
Runnable r2 = () -> System.out.println("Hello from Lambda!");

public class Test {
    public static void main(String[] args) {
        new Thread(r1).start();
        new Thread(r2).start();
    }
}

Explanation:

  • Runnable r1 → implemented using anonymous inner class.

  • Runnable r2 → implemented using Lambda expression.

  • Both can be passed to a thread.

Output

Hello from Runnable!
Hello from Lambda!

👉 Why important? Lambdas make code concise and readable, reducing boilerplate code.


Q3. What are Functional Interfaces? Can you create your own?

A Functional Interface is an interface with exactly one abstract method (Single Abstract Method – SAM).

They are the foundation for Lambdas and Method References.

Example

@FunctionalInterface
interface MyFunctionalInterface {
    void sayHello(); // Single Abstract Method
}

public class Test {
    public static void main(String[] args) {
        MyFunctionalInterface fi = () -> System.out.println("Hello Functional Interface!");
        fi.sayHello();
    }
}

Explanation:

  • @FunctionalInterface → ensures only one abstract method.

  • sayHello() → the SAM implemented using Lambda.

  • fi.sayHello(); → executes the Lambda.

Output

Hello Functional Interface!

👉 Note: Java 8 provides built-in functional interfaces in java.util.function such as:

  • Predicate<T> → boolean test condition.

  • Function<T,R> → transforms input to output.

  • Consumer<T> → consumes input, no return.

  • Supplier<T> → supplies a value.


Q4. What is the difference between Functional Interface and Marker Interface?

Aspect

Functional Interface

Marker Interface

Abstract Methods

Exactly 1

0

Purpose

Used for Lambda Expressions

Used for marking/metadata

Examples

Runnable, Callable, Predicate

Serializable, Cloneable

👉 Takeaway:

  • Functional Interface → enables functional programming.

  • Marker Interface → provides type information to JVM (no behavior).


Q5. Explain the usage of @FunctionalInterface annotation.

The @FunctionalInterface annotation is used to ensure an interface has exactly one abstract method.

Example

@FunctionalInterface
interface Calculator {
    int add(int a, int b);

    // Uncommenting the below line will cause a compile-time error
    // int subtract(int a, int b);
}

public class Test {
    public static void main(String[] args) {
        Calculator c = (a, b) -> a + b;
        System.out.println("Sum: " + c.add(5, 3));
    }
}

Explanation:

  • Calculator has a single abstract method → add(int a, int b).

  • If we try to add another abstract method, the compiler throws an error.

  • This ensures the interface can be used with Lambdas.

Output

Sum: 8

👉 Why important? Prevents accidental addition of multiple abstract methods, which would break Lambda usage.


Q6. What are Default Methods in Interfaces? Why were they introduced?

A default method in an interface is a method with a body, introduced in Java 8.They were added to provide backward compatibility so that interfaces can evolve without breaking existing implementations.

Example

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

class Car implements Vehicle {}

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

Explanation:

  • default void start() → provides an implementation inside the interface.

  • Car does not override start(), but it inherits the default method.

  • Default methods help Java maintain compatibility when new methods are added to interfaces like List, Map, etc.

Output

Vehicle is starting...

👉 Key Point: Default methods allow Java interfaces to evolve gracefully without forcing all implementing classes to change.


Q7. Can we override Object class methods inside interfaces as default methods?

No ❌. Methods like toString(), equals(), and hashCode() from Object cannot be overridden as default methods in an interface.

Why?

  • Every Java class inherits Object.

  • If interfaces were allowed to override these, it would create ambiguity for classes that already inherit them.

👉 Key Point: Java prevents overriding Object class methods in interfaces to avoid conflicts.


Q8. What are Static Methods in Interfaces? How do they differ from Default Methods?

  • Static methods were also introduced in Java 8.

  • Unlike default methods, static methods belong to the interface itself (not to implementing classes).

Example

interface MathUtils {
    static int square(int x) {
        return x * x;
    }
}

public class Test {
    public static void main(String[] args) {
        System.out.println(MathUtils.square(5));
    }
}

Explanation:

  • static int square(int x) → utility method inside an interface.

  • Called using InterfaceName.methodName().

  • Cannot be inherited by implementing classes.

Output

25

👉 Key Point:

  • Default methods → inherited by implementing classes.

  • Static methods → must be accessed using the interface name.


Q9. Explain Method References with examples.

A Method Reference is a shorthand notation for calling a method using a Lambda.

Types of Method References

  1. object::instanceMethod

  2. Class::staticMethod

  3. Class::new (constructor reference)

Example

import java.util.*;

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

        // Using Lambda
        names.forEach(n -> System.out.println(n));

        // Using Method Reference
        names.forEach(System.out::println);
    }
}

Explanation:

  • names.forEach(n -> System.out.println(n)); → Lambda version.

  • names.forEach(System.out::println); → Method Reference (shorthand).

  • Both achieve the same result, but method references improve readability.

Output

John
Alice
Bob

👉 Key Point: Method references are a cleaner, more readable replacement for certain Lambdas.


Q10. What is the difference between Lambda Expression and Anonymous Inner Class?

Both Lambdas and anonymous inner classes can implement functional interfaces, but they have key differences.

Example

public class Test {
    public static void main(String[] args) {
        Runnable r1 = () -> System.out.println("Lambda Run");

        Runnable r2 = new Runnable() {
            public void run() {
                System.out.println("Anonymous Class Run");
            }
        };

        new Thread(r1).start();
        new Thread(r2).start();
    }
}

Explanation:

  • r1 → Lambda implementation (concise).

  • r2 → Anonymous class implementation (verbose).

  • Both create Runnable instances but differ in performance and syntax.

Output (order may vary due to threading)

Lambda Run
Anonymous Class Run

Comparison Table

Aspect

Lambda Expression

Anonymous Inner Class

Syntax

Concise, functional style

Verbose

this reference

Refers to enclosing class

Refers to the anonymous class

Performance

Lightweight (JVM uses invokedynamic)

Creates a new class at runtime

Best use case

Single abstract method (functional interface)

When multiple methods/state are needed

👉 Key Point: Use Lambda when you need a functional-style one-liner. Use anonymous class when you need stateful or multiple methods.


Final Thoughts

In this post, we covered the first 5 Java 8 interview questions:

  1. New features in Java 8

  2. Lambda expressions

  3. Functional interfaces

  4. Difference between functional and marker interfaces

  5. @FunctionalInterface annotation

  6. Default methods in interfaces

  7. Why Object methods can’t be overridden in interfaces

  8. Static methods in interfaces

  9. Method references

  10. Difference between Lambdas and Anonymous classes


These form the foundation of Java 8 interviews. If you are strong in these basics, you can confidently move on to Streams API, Optional, and real-world coding problems.



Commentaires


bottom of page