Top Java8 Lambda Interview Questions (2021) | CodeUsingJava








Top Java8 Lambda Interview Questions (2021)


  1. What is the lambda expression in Java? Give syntax?
  2. What are the characteristics of lambda expression?
  3. What are the features of lambda expression?
  4. What is functional interface?
  5. How lambda expression and functional interfaces are related?
  6. Explain various forms of writing lambda expression?
  7. What is method reference in java8 ?
  8. What do you understand by block lambda expression?
  9. Will lambda expression creates an object whenever it's executed?
  10. Give an example of recursive lambda expression?

What is the lambda expression in Java? Give syntax?

Lambda expressions are functions with no name and identifiers.it is itself an anonymous method.it promotes functional interface and only implementa abstract functions.
Syntax:parameter -> expression body
  • Argument-list: It can be both empty and non-empty.
  • The arrow-token is used to connect the arguments-list and expression body.
  • Body: It contains lambda expression statements and expressions
    For example:(a,b)-<System.out.print(a+b);


What are the characteristics of lambda expression?

  • Optional type declaration-A parameter's type does not need to be declared. The compiler infers the same from the parameter's value.
  • Optional return keyword-A single parameter does not need to be declared in parentheses. Parentheses are necessary for multiple parameters.
  • Optional parenthesis around arguements-if the body contains a single statement,it is not necessary to use curly braces in the expression body.
  • Optional curly braces-If body contains single expression to return value , the compiler returns it automatically. To show that an expression returns a value, curly brackets are required.

What are the features of lambda expression?

Lambda expressions are similar to methods. It's commonly used to implement simple event listeners in functional programming with the Java Streams API. Lambda expression reduces the complexity of creating anonymous inner classes.

What is functional interface?

functional interface contains only one abstract method.it can have any number or static methods along with object class method.Java provides predefined functional interfaces .For examples:Runnable,ActionListenener,Comparable etc.
   
   @FunctionalInterface
   interface i1{
    void show(String s);
   }
public class demo implements i1 {
    public void show(s){
        System.out.println(s);
    }
public static void main(String[]args)
{
    demo d=new demo();
    d.show("lambda expresssion");
}            
}
Here is only one abstract method in interface i1 so this interface is known as functional interface and by default it is a public abstract method in your interface. 


How lambda expression and functional interfaces are related?

functional expresssion provides reference to lambda expression.Functional inerface specifies target type of lambda expresion.
interface i1{
 void display();
}

public class demo(){
public static void main(Strings[]args)
i1 i=()->System.out.print("lambda expression");
i.display();
}
 

Explain various forms of writing lambda exppression?

lambda expression can be
  • Zero parameter
    In zero parameter ,we don't pass any parameter
      () -> System.out.println("Zero parameter lambda");
     
    Example:
     interface i1{  
        public String show();  
    }  
    public class Lambda{  
    public static void main(String[] args) {  
       i1 i=()->{  
            return "I have nothing to say.";  
        };  
        System.out.println(i.show());  
    }  
    }  
    
  • One parameter
    In one parameter ,we can passone single parameter
      (p) -> System.out.println("one parameter lambda");
     
    Example:
    interface i1{  
        public String show(String msg);  
    }  
      
    public class Lambda{  
        public static void main(String[] args) {  
          
            // Single-parameter lambda expression.  
           i1 x=(msg)->{  
                return "Hello, "+msg;  
            };  
            System.out.println(s1.show("Sonoo"));  
              
            // The function parentheses can be omitted.
           i1 s2= msg ->{  
                return "Hello, "+msg;  
            };  
            System.out.println(s2.show("Sonoo"));  
        }  
    }    
    
  • Multiple parameter
    In multiple parameter ,we can pass multiple parameter
      (x,y) -> System.out.println("multiple parameter lambda"+x+""+y);
     
    Example:
    interface i1{  
        int sum(int x,int y);  
    }  
      
    public class Lambda{  
        public static void main(String[] args) {  
              
            // lambda expression with Multiple parameters   
            i1 a=(x,y)->(x+y);  
            System.out.println(a.sum(10,20));  
              
            // Multiple parameters with data type in lambda expression  
            i1 b=(int x,int y)->(x+y);  
            System.out.println(b.sum(100,200));  
        }  
    }     
    

What is method reference in java8?

It is a replacement or alternative of the lambda expression. It is used to refer functional interface methods for an existing method. We can use this when we have an existing implementation of the abstract method of our functional interface.It's a simple and concise lambda expression.
Types of method reference
  • Reference to a static method.
  • Reference to an instance method.
  • Reference to a constructor.


What do you understand by block lambda expression?


Java contains blocks of code with more than one statement. Block Lambda contains various operations that have many statements. It consists of variables, loops, conditional statements, etc. This is achieved by enclosing a block of statements in lambda body with braces{}. Lambda expressions are code blocks that include many statements. This sort of Lambda Body is known as a "Block Body.".Lambdas with block bodies are referred to as "Block Lambdas." It's even possible to include a return statement, such as return value.

Will lambda expression creates an object whenever it's executed?

No, only one instance will be created it creates singletons for all expressions that don't capture values. On every evaluation, a new object need not be allocated. Objects created by different lambda expressions do not have to be of the same class. Each object created by evaluation does not have to be of the same class. It is not necessary to create a previous lambda evaluation if an existing instance is already present.

Give example of recursive lambda expresssion?


public class demo {

    public static void main(String[] args) {

        class test {
            final UnaryOperator<Integer> a = i -> i == 0 ? 1 : i * this.a.apply( i - 1);
        }

       System.out.println(new test().a.apply(5));
    }
}