Adblocker detected! Please consider whitelist or disable this site.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

To keep the site operating, we need funding, and practically all of it comes from internet advertising.

If you still want to continue, Please add techgeeknext.com to your ad blocking whitelist or disable your adblocking software.

×
Essential Java 8 Interview Questions and Answers (2024) | TechGeekNxt >>


Java 8 Interview Questions and Answers

You can also go through Java 8 Programming Interview Questions and Answers with the following questions.

Q: What's new features added in Java 8?
Ans:

  • Lambda Expressions
  • Functional Interfaces
  • Stream API
  • Date and Time API
  • Interface Default Methods and Static Methods
  • Spliterator
  • Method and Constructor References
  • Collections API Enhancements
  • Concurrency Utils Enhancements
  • Fork/Join Framework Enhancements
  • Internal Iteration
  • Parallel Array and Parallel Collection Operations
  • Optional
  • Type Annotations and Repeatable Annotations
  • javac Enhancements
  • JVM Changes
  • Java 8 Compact Profiles: compact1,compact2,compact3
  • JDBC 4.2
  • JAXP 1.6
  • Java DB 10.10
  • Networking
  • Security Changes
  • Method Parameter Reflection
  • Base64 Encoding and Decoding
  • IO and NIO2 Enhancements
  • Nashorn JavaScript Engine

Q: What is Method Reference in Java 8?
Ans:

A Method Reference is used to reference a method without invoking, it is used as Lambda Expressions for treating methods.
A reference method can be defined by a double colon separating the name of a class or object and the method name. It has various variations, such as the reference constructor
EXAMPLE
(o) -> o.toString(); // without Method Reference
Object::toString();  //with Method Reference

Q: What is Lambda Expression?
Ans:

Lambda expression is an important feature introduced in Java SE 8. Lambda Expression is an anonymous function that accepts a set of input parameters and returns results.
The Lambda expression provides the implementation of an interface which has functional interface, which reduce lots of code.
EXAMPLE
Without Lambda Expression
interface Paint{
    public void color();
}
public class Example {
    public static void main(String[] args) {
        String color="red";

        //without lambda, Paint implementation using anonymous class  
        Paint p=new Paint(){
            public void color(){System.out.println("coloring "+red);}
        };
        p.color();
    }
}

With Lambda Expression
@FunctionalInterface
interface Paint{
    public void color();
}

public class Example {
    public static void main(String[] args) {
        String color="red";

        //with lambda expression 
        Paint p=()->{
            System.out.println("Coloring "+color);
        };
        p.color();
    }
}  

Q: What is Java Lambda Expression Syntax?
Ans:

(parameter-list) -> {Lambda Expression Body}  
It has 3 parts:
  1. Parameter List - It's optional, can have zero or more parameters.
  2. Lambda Arrow Operator - "->" it's called Arrow Token, used to link parameter-list and body.
  3. Lambda Expression Body - It contains expressions and statements.
EXAMPLE
//It's "java.lang.Runnable" Functional Interface, as it doesn't have any parameter and return result.
() -> System.out.println("Hello");

Q: Can it be possible to define our own Functional Interface?
Ans:

Yes, we can define our own Functional Interface by using @FunctionalInterface annotation.

Q: What is Functional Interface? Why there is a need to have Functional Interface?
Ans:

Functional Interface is an interface with one and only one abstract method. If we define an Interface as Functional Interface with @FunctionalInterface annotation, it becomes mandatory to have one and only one abstract method. It's not necessary to have Functional Interface, however if we are using Lambda expression , it means we are using Functional Interface.

Q: What are the rules of Functional Interface?
Ans:

Below are the rules to define Functional Interface.
  • Only one abstract method is allowed to define in Functional Interface.
  • Use @FunctionalInterface annotation to declare Functional Interface.
  • The functional interface defines an abstract method that overrides one of the public methods from java.lang.Object already considered to be a functional interface. The purpose is that every implementation class on this interface can be implemented for this abstract method either from a superclass or specified by the implementation class itself..
  • Can have any number of methods default methods

Q: What are Functional Interfaces present in the Standard Library?
Ans:

The java.util.function package contains a lot of functional interfaces, the more common are but not limited to.
  1. Function
    It takes one parameter and returns a result
  2. Signature
    R apply(T t)	
    Example
    String::toLowerCase, Math::tan
  3. Consumer
    It takes one parameter and returns no result
  4. Signature
    void accept(T t)	
    Example
    System.out::println, Error::printStackTrace
    
  5. Predicate
    It takes one parameter and returns a boolean
  6. Signature
    boolean test(T t, U u)	
    Example
    String::isEmpty, Character::isDigit
  7. Supplier
    It takes no parameter and returns a result
  8. Signature
    T get()	
    Example
    LocalDate::now, Instant::now
    
  9. BiFunction
    It takes two parameters and returns a result
  10. Signature
    R apply(T t, U u);
  11. BinaryOperator
    Same as BiFunction, it takes two parameters and returns result. The two parameters and the result are all of the same types
  12. Signature
    T apply(T t1, T t2)
    Example
    BigInteger::add, Math::pow
    
  13. UnaryOperator
    Same as Function, it takes single parameter and returns result of the same type
  14. Signature
    T apply(T t)	
    Example
    String::toLowerCase, Math::tan
    

Checkout our related posts :

Q: What is Optional and how to use?
Ans:

Java 8 has launched a new Optional class in java.util package, used to indicate a value that is present or absent.
Advantages of using Optional:
  1. Null checks not required.
  2. It avoids NullPointerException at run-time.
  3. Can develop clean code without boiler plate code.
Optional.ofNullable() - if value is present in the given object, it returns a non-empty Optional else returns empty Optional.
Optional.empty() method is useful to create an empty Optional object.
EXAMPLE
Optional<String> color = Optional.of("BLUE");
System.out.println("non empty Optional object: " + color);
System.out.println("non empty Optional object value: " + color.get());
color.ifPresent(c -> System.out.println("Color is available."));

--------------------------------------------------------
Output
--------------------------------------------------------
non empty Optional object: Optional[BLUE]
non empty Optional object value: BLUE
Color is available.

Q: What is Default Method?
Ans:

Java 8 introduces the Default Method are also known as Defender Methods or Virtual Extension Methods feature that allows developers to add new methods to the interfaces without breaking their existing implementation.
interface DefaultInterfaceExample
{
    // abstract method 
    public void add(int a);

    // default method 
    default void display()
    {
      System.out.println("Default Method");
    }
}

class CalculationClass implements DefaultInterfaceExample
{
    // implementation of add abstract method 
    public void add(int a)
    {
        System.out.println(a+a);
    }

    public static void main(String args[])
    {
        CalculationClass c = new CalculationClass();
        c.add(6);

        // call default method 
        c.display();
    }
}


-------------------------------------------------------
OUTPUT
-------------------------------------------------------
12
Default Method

Q: Can Interface have Static Methods same as static method of classes?
Ans:

Yes, interface can have Static Methods same as static method of classes.
EXAMPLE
interface StaticInterfaceExample
{
    // abstract method 
    public void add(int a);

    // static method 
    static void display()
    {
      System.out.println("Static Method");
    }
}

class CalculationClass implements StaticInterfaceExample
{
    // implementation of add abstract method 
    public void add(int a)
    {
        System.out.println(a+a);
    }

    public static void main(String args[])
    {
        CalculationClass c = new CalculationClass();
        c.add(6);

        // call static method 
        d.display();
    }
}


-------------------------------------------------------
OUTPUT
-------------------------------------------------------
12
Static Method
















Recommendation for Top Popular Post :