Lambda Expressions – Interview Question

Collection of typically Interview Questions on lambda expression for interview preparation. you can also read more about Lambda expression Alternatively, you can also test your knowledge by an online free quiz for a lambda expression.

Interview Question: What is Lambda expression?

Answer: Lambda Expressions are nameless functions given as constant values. They can appear anywhere that any other constant may, but are typically written as a parameter to some other function. The canonical example is that you’ll pass a comparison function to a generic “sort” routine, and instead of going to the trouble of defining a whole function (and incurring the lexical discontinuity and namespace pollution) to describe this comparison, you can just pass a lambda expression describing the comparison.

Interview Question: Why should we use a lambda expression?


Lambda expressions are a simpler syntax for anonymous delegates and can be used everywhere an anonymous delegate can be provides a shorthand for the compiler to emit methods and assign them to delegates; alternatively, lambda expressions can be converted to expression trees which allows for a lot of the magic like LINQ to SQL.
For example, suppose we need to compare sort an array of strings by string length? we need to write a method for that. Put that method into one of the classes, follow some design practice to design a class all of this adds up. But with a lambda expression, we can achieve a similar result with inline code or shortcode

Interview Question: What is lambda operator (=>) use or indicates?

Answer: All lambda expressions use the lambda operator =>, which is read as “goes to”. The left side of the lambda operator specifies the input parameters (if any) and the right side holds the expression or statement block. The lambda expression x => x * x is read “x goes to x times x.” This expression can be assigned to a delegate type as follows:

Func<int, int> square = x => x * x;

Interview Question: What is difference between lambda and LINQ?

Answer: Language-Integrated Query (LINQ) is a set of features introduced in Visual Studio 2008 that extends powerful query capabilities to the language syntax of C# and Visual Basic

A lambda expression is an anonymous function that you can use to create delegates or expression tree types. By using lambda expressions, you can write local functions that can be passed as arguments or returned as the value of function calls.

new [] { "Dan", "Yossi", "Ben" }.Where(item => item.Length == 3);

Lambda expression

item => item.Length == 3


(from item in (new [] { "Dan", "Yossi", "Ben" }) where item.Length == 3)

Interview Question: What is difference between lambda expressions and anonymous methods?

Answer: A lambda expression is simply shortcut syntax for an anonymous method. Anonymous methods look like this:

delegate(params) {method body}

The equivalent lambda expression would look like this:

params => method body

In short, all lambda expressions are anonymous methods, but it is possible to have an anonymous method that is not written in lambda syntax (like the first example above).

you can also read more about lambda expression. Alternatively, you can also test your knowledge by an online free quiz for a lambda expression.

Leave a Reply

Your email address will not be published. Required fields are marked *