An expression-bodied lambda is a concise way to write a lambda expression using a single expression without braces and return keyword. It returns the value of that expression directly.
Click to reveal answer
beginner
How do you write a simple expression-bodied lambda that adds two integers a and b?
You write it as: (a, b) => a + b;
Click to reveal answer
intermediate
What is the difference between a statement-bodied lambda and an expression-bodied lambda?
A statement-bodied lambda uses braces and can have multiple statements, requiring a return keyword if it returns a value. An expression-bodied lambda has no braces and returns the result of a single expression directly.
Click to reveal answer
intermediate
Can expression-bodied lambdas be used with void-returning methods?
Yes, expression-bodied lambdas can be used with void-returning methods if the expression is a statement like a method call. For example: x => Console.WriteLine(x);
Click to reveal answer
beginner
Why are expression-bodied lambdas useful?
They make code shorter and easier to read when the lambda only needs to return the result of a simple expression.
Click to reveal answer
Which of the following is a valid expression-bodied lambda in C#?
A(x, y) => x * y
B(x, y) => { return x * y; }
C(x, y) => { x * y; }
D(x, y) => return x * y;
✗ Incorrect
Option A is an expression-bodied lambda with a single expression. Option B is a statement-bodied lambda. Option C is invalid because it lacks return. Option D is invalid syntax.
What does this expression-bodied lambda do? x => x + 1
AReturns x minus 1
BReturns x plus 1
CPrints x plus 1
DDoes nothing
✗ Incorrect
The lambda takes x and returns x plus 1.
Can expression-bodied lambdas contain multiple statements?
AYes, always
BOnly if inside braces
CYes, if separated by semicolons
DNo, only one expression
✗ Incorrect
Expression-bodied lambdas can only have one expression. Multiple statements require braces and become statement-bodied lambdas.
Which keyword is NOT needed in an expression-bodied lambda?
Areturn
B=>
Clambda parameters
Dexpression
✗ Incorrect
Expression-bodied lambdas return the expression value implicitly, so 'return' is not used.
How would you write a void expression-bodied lambda that prints a message?
Ax => return Console.WriteLine(x)
Bx => { Console.WriteLine(x); }
Cx => Console.WriteLine(x)
Dx => Console.WriteLine(x); return;
✗ Incorrect
Option C is a valid void expression-bodied lambda. Option B is statement-bodied. Options A and D are invalid syntax.
Explain what an expression-bodied lambda is and how it differs from a statement-bodied lambda.
Think about how many statements each can have and how they return values.
You got /5 concepts.
Write an example of an expression-bodied lambda that takes two numbers and returns their product.
Use => with a simple multiplication expression.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using an expression-bodied lambda in C#?
easy
A. To write a short function with a single expression without braces or return keyword
2. Which of the following is the correct syntax for an expression-bodied lambda that adds two integers a and b?
easy
A. (a, b) => a + b
B. (a, b) => a + b;
C. (a, b) => { return a + b; }
D. (a, b) => { a + b }
Solution
Step 1: Identify expression-bodied lambda syntax
Expression-bodied lambdas use => followed by a single expression without braces or return keyword.
Step 2: Check each option
(a, b) => a + b correctly uses (a, b) => a + b without braces or return. (a, b) => { return a + b; } uses braces and return (not expression-bodied). (a, b) => a + b; has a semicolon after expression (not needed). (a, b) => { a + b } uses braces without return (invalid).
Hint: No braces or return keyword in expression-bodied lambdas [OK]
Common Mistakes:
Adding braces and return keyword
Including semicolon after expression
Using braces without return keyword
3. What is the output of the following C# code?
Func<int, int> square = x => x * x;
Console.WriteLine(square(4));
medium
A. 8
B. 4
C. 16
D. Compilation error
Solution
Step 1: Understand the lambda function
The lambda expression x => x * x defines a function that returns the square of x.
Step 2: Evaluate the function call
Calling square(4) computes 4 * 4 = 16.
Final Answer:
16 -> Option C
Quick Check:
4 squared = 16 [OK]
Hint: Multiply input by itself to get square [OK]
Common Mistakes:
Confusing output with input
Expecting compilation error
Misreading the lambda expression
4. Identify the error in the following expression-bodied lambda:
Func<int, int> increment = x => { x + 1; };
medium
A. Semicolon after expression is invalid
B. Braces are not allowed in expression-bodied lambdas
C. Lambda must have two parameters
D. Missing return keyword inside braces
Solution
Step 1: Analyze the lambda syntax
The lambda uses braces, so it must have an explicit return statement to return a value.
Step 2: Identify the missing return
Inside braces, the expression x + 1 is evaluated but not returned, causing a compile error.
Final Answer:
Missing return keyword inside braces -> Option D
Quick Check:
Braces require return keyword [OK]
Hint: Use return inside braces or remove braces for expression-bodied lambda [OK]
Common Mistakes:
Assuming braces are disallowed
Thinking semicolon after expression is invalid
Expecting multiple parameters
5. Given a list of integers, which expression-bodied lambda correctly filters out even numbers and returns their squares?
var numbers = new List<int> {1, 2, 3, 4, 5};
var result = numbers.Where( /* lambda here */ ).Select(x => x * x).ToList();
hard
A. x => x % 2 == 0
B. x => x % 2 != 0
C. x => { return x % 2 != 0; }
D. x => { x % 2 != 0; }
Solution
Step 1: Understand filtering condition
We want to keep odd numbers, so the condition is x % 2 != 0.
Step 2: Choose correct lambda syntax
x => x % 2 != 0 uses expression-bodied lambda without braces, correctly returning the condition. x => x % 2 == 0 keeps even numbers (wrong). x => { return x % 2 != 0; } uses braces with return (valid but not expression-bodied). x => { x % 2 != 0; } uses braces without return (invalid).
Final Answer:
x => x % 2 != 0 -> Option B
Quick Check:
Filter odds with expression-bodied lambda [OK]
Hint: No braces and return for expression-bodied lambdas [OK]
Common Mistakes:
Using braces without return
Choosing wrong condition for filtering
Mixing expression-bodied with block-bodied syntax
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.