Java - Interview Questions and Answers for 'Lambda expression' | Search Java Interview Question - javasearch.buggybread.com
Javasearch.buggybread.com
Share

Search Java Interview Questions


 2098 questions in repository.
 There are more than 200 unanswered questions.
Click here and help us by providing the answer.
Label / Company      Label / Company / Text

   



Interview Questions and Answers for 'Lambda expression' - 9 question(s) found - Order By Newest

  Q1. What is a Lambda Expression ? What's its use ?Core Java
Admin
info@buggybread.com
Ans. Its an anonymous method without any declaration.

Lambda Expression are useful to write shorthand Code and hence saves the effort of writing lengthy Code.

It promotes Developer productivity, Better Readable and Reliable code.

  Sample Code for lambda

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   java8   lambda expression   architecture     Axxess  CACI  ZonOff  Dell India  Wellington Management  N26  Lithium Technologies      expert        frequent

Try 1 Question(s) Test


Not frequently asked as it was introduced with Java 8.
 Q2. Difference between final and effectively final ? Why is effectively final even required ?Core Java
Admin
info@buggybread.com
Ans. Final variable means a variable that has been declared final and hence cannot be de referenced after initialization. Effective final means a variable that has not been declared final but haven't been reassigned the value after initialization.

First is the regulation that restricts the reassignment and will raise a compilation error if we try to do so. Second is the outcome without the restriction.

Effective Final is the eventual treatment of the variable that is required for many features. For eq - Java 8 requires that local variables referenced from a lambda expression must be final or effectively final.It means all local referenced from lambda expressions must be such that their value shouldn't be changed after initialization whether declared final or not.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   java8   java 8   final   effective final   final vs effective final   lambda expressions      expert

Try 2 Question(s) Test


 Q3. Difference between Predicate, Supplier and Consumer ? Core Java
Admin
info@buggybread.com
Ans. Predicate represents an anonymous function that accepts one argument and produces a result.

Supplier represents an anonymous function that accepts no argument and produces a result.

Consumer represents an anonymous function that accepts an argument and produces no result.

  Sample Code for Predicate

  Sample Code for Supplier

  Sample Code for Consumer

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   java8   predicate   consumer   supplier   lambda expression      expert


 Q4. What does the following lambda expression means ?

helloJava8 ( x-> x%2 )
Admin
info@buggybread.com
Ans. helloJava8 receives an Integer as argument and then returns the modulus of that Integer.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   java8   lambda expression


Rarely asked as it was introduced with Java 8.
 Q5. Difference between DoubleSummaryStatistics , IntSummaryStatistics and LongSummaryStatistics ?Core Java
Admin
info@buggybread.com
Ans. They all does the same task i.e to compute statistical information on the stream of data. They differ by the way they store the statistical information as they expect a different data type of the values being used.

IntSummaryStatistics and LongSummaryStatistics expect non floating point values and hence stores the statistical information like min,max and sum as non floating values ( int or long ) whereas DoubleSummaryStatistics stores these information as floating value.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   java8   java 8   lambda expressions   doublesummarystatistics   intsummarystatistics   longsummarystatistics   summarystatistics


Rarely asked as it was introduced with Java 8.
 Q6. What is the use of :: operator wef from Java 8 ?Core Java2016-07-25 13:27:57

Ans. We can refer to a function using this operator like System.out.println(intList.stream().reduce(Math::max).get());

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     :: operator   lambda expression  java 8


 Q7. What is a method reference in Java ?Core Java2017-01-29 13:55:22

Ans. Introduced with java 8 , Method References help us to point to methods by their name.

A method references is described using :: symbol

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     method reference  java 8  lambda expressions


 Q8. Lets say we have a set "set1" with objects of Class A, Class has a non static string called element1, Write a code ( using lambda expression ) to see if there is any object in the set with element1 = "Hello". Core Java2017-08-06 11:51:10

Ans. set1.stream().filter(p->p.element1.equals("Hello")).findAny().isPreent();

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     lambda expressions  java 8  java8  java 8 filter  findAny()


 Q9. Write a Program that gets a set of numbers , filters out the non prime numbers , calculate the factorial of each prime number and then finds the average of all factorials using Lambda expressions2017-05-14 14:02:57

Ans. public class BuggyBread1 {
   
   public static void main(String args[]) {
      
      // Declare and Initialize the Collection
      Set<Integer> intSet = new HashSet<Integer>();

      // Add Elements
      intSet.add(1);
      intSet.add(2);
      intSet.add(3);
      intSet.add(4);
      intSet.add(5);

      double averageOfNonPrimeFactorials = intSet.stream().filter(p->checkIfPrime(p)).collect(Collectors.averagingInt(p->calculateFactorial(p)));
      
      System.out.println(averageOfNonPrimeFactorials );
   }
   
   static private boolean checkIfPrime(int num){
      for(int count=2;count < num;count++){
         if(num % count == 0){
            return false;
         }
      }
      return true;
   }
   
   static private int calculateFactorial(int num){
      int factorial = 1;
      for(int count=num;count > 0;count--){
         factorial = factorial * count;
      }
      return factorial;
   }
}

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java 8  lambda expressions  lambda  filter  Collectors  factorial  prime number




Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner



comments powered by Disqus
 

Help us and Others Improve. Please let us know the questions asked in any of your previous interview.

Any input from you will be highly appreciated and It will unlock the application for 10 more requests.

Company Name:
Questions Asked:
         

X Close this

X Close this

Help Us Improve.
Please share your
interview experience.

Company Name:   


Questions Asked: