Java - Lambda Expressions - Interview Questions

What are Lambda expressions in Java programming language?

 FAQ

Lambda expression is a Java programming language feature introduced in Java 8 that provides functional programming constructs to the Java programming language, which simplifies the Java code in certain cases such as with Java anonymous inner classes. Lambda expressions blends functional programming features with object oriented programming features of Java resulting in simplified and more powerful concurrency features.

Lambda expressions are blocks of Java code that can be defined and passed as data which can be executed at a later time.

What are functional interfaces?

 FAQ

Functional interfaces are Java interfaces which have only one declared (abstract) method. Functional interfaces can have other implemented methods such as default methods and static methods. Some of the common functional interfaces in Java are Runnable, Comparable, ActionListner etc.

Functional interfaces are generally implemented as inner classes and anonymous inner classes, which results in bulky code with many lines. This is sometimes referred to as 'Vertical' problem.

Lambda expressions are used to implement functional interfaces which define a single method. Lambda expressions avoid the vertical problem by simplifying the code of the traditional inner classes and anonymous inner classes.

What pre Java 8 code does Lambda expression simplify?

 FAQ

Lambda expression simplifies the inner class and anonymous inner class code, which usually suffer from the 'vertical' problem (Too many lines of code required to implement a basic logic). Lambda expressions avoid the 'vertical' problem by simplifying and reducing the number of lines required to implement the inner class functionality.

Can lambda expressions be used to implement interfaces having default and static methods?

 FAQ

Lambda expressions can be used implement interfaces having default and static methods only if there is a single abstract method in the interface. This called a functional interface.

From Java 8 onwards, an interface can contain default methods and static methods whose implementation is defined directly in the interface declaration.

What is the syntax of lambda expressions in Java programming language?

 FAQ

A lambda expression consists of three parts

First - A parenthesized set of parameters, Second - An arrow pointing to right, Third - A body, which can be a block of Java code or a single expression.

//Passes an integer argument i, and returns 10+i
(int i) -> 10+i;
//Passes no arguments and returns 50
() -> 50;
//Passes string argument s and returns nothing
(String s) -> {system.out.println(s);}
Java Interview Guide has over 250 REAL questions from REAL interviews. Get the guide for $15.00 only.
 
BUY EBOOK
 

How many parameters can a lambda expression have?

 FAQ

A lambda expression can have zero, one or multiple parameters.

//Zero parameter lambda expression
() -> System.out.println('No parameters');
//One parameter lambda expression
(i) -> i*10;
//Multiple parameter lambda expression
(i1, i1) -> System.out.println(i1+i2);

Write a functional interface and implement it using non-lambda code and lambda expression

 FAQ

This is a good exercise to clearly understand the concepts and advantages of lambda expressions. In below example we declare a functional interface 'EventChangeListener' that has one abstract method 'onChange()'. We then implement the interface to print the text 'Event Changed' when the onChange() method is invoked. For comparison, we implement the code using non-lambda code as well as with lambda expression...

*** See complete answer and code snippet in the Java Interview Guide.

What criteria must be met to match a lambda expression to an interface?

 FAQ

Following three criteria must be met to match a lambda expression to an interface.

1. The interface must have one (and only one) abstract method...

*** See complete answer and code snippet in the Java Interview Guide.

What is the major difference between lambda expression vs anonymous interface implementation with regards to state?

 FAQ

Anonymous interface implementations can have state (member variables), whereas lambda expressions are stateless...

*** See complete answer and code snippet in the Java Interview Guide.

Do you need to specify the type for parameters in a lambda expression?

 FAQ

In most cases you do not need to specify the type for parameters in a lambda expression. The java compiler infers the type for the parameters by matching them with the parameter types of the abstract method of the functional interface.

For example, in below lambda expression ...

*** See complete answer and code snippet in the Java Interview Guide.

Can you have multiple lines of code in a lambda function body?

 FAQ

Yes, lambda function body can have multiple lines of code within curly braces {}...

*** See complete answer and code snippet in the Java Interview Guide.

Java Interview Guide has over 250 REAL questions from REAL interviews. Get the guide for $15.00 only.
 
BUY EBOOK
 
 
Java Interview Guide

$15.00

BUY EBOOK
  SSL Secure Payment
Java Interview Quesiuons - Secure Payment
Java Interview Guide

$15.00

BUY EBOOK
  SSL Secure Payment
Java Interview Quesiuons - Secure Payment
 

Java - Interview Questions

Java - Object Oriented ProgrammingJava - Objects & ClassesJava - Data TypesJava - VariablesJava - StringsJava - ArraysJava - CollectionsJava - ReflectionJava - Lambda ExpressionsJava - StreamsJava - GenericsJava - ExceptionsJava - IOJava - ThreadsJava - ConcurrencyJava - JDBCJava - NetworkingJava - SecurityJava - JVM InternalsJava - PerformanceJava - New in Java 8Java - New in Java 9Java - New in Java 10Java - New in Java 11
 
MASTER Java  

Top ranked courses to help you master Java skills.
Java Programming Masterclass

iconicon

Offered By - Tim Buchalka
Platform - Udemy
Rating - * * * * *
Students Enrolled - 575,000 +

RECOMMENDED RESOURCES
Behaviorial Interview
Top resource to prepare for behaviorial and situational interview questions.

STAR Interview Example