Challenge - 5 Problems
Lambda Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple lambda expression
What is the output of this C# code snippet?
C Sharp (C#)
Func<int, int> square = x => x * x; Console.WriteLine(square(5));
Attempts:
2 left
💡 Hint
The lambda takes an integer and returns its square.
✗ Incorrect
The lambda expression x => x * x takes an integer x and returns x multiplied by itself. So square(5) returns 25.
❓ Predict Output
intermediate2:00remaining
Lambda with multiple parameters and statement body
What will this code print?
C Sharp (C#)
Func<int, int, int> addAndMultiply = (a, b) => { int sum = a + b; return sum * 2; }; Console.WriteLine(addAndMultiply(3, 4));
Attempts:
2 left
💡 Hint
The lambda sums two numbers and then doubles the result.
✗ Incorrect
The lambda adds 3 and 4 to get 7, then multiplies by 2 to get 14.
❓ Predict Output
advanced2:00remaining
Lambda capturing outer variable
What is the output of this code?
C Sharp (C#)
int factor = 3; Func<int, int> multiply = x => x * factor; factor = 5; Console.WriteLine(multiply(4));
Attempts:
2 left
💡 Hint
Lambdas capture variables by reference, not by value.
✗ Incorrect
The lambda captures 'factor' by reference. Since 'factor' is changed to 5 before calling multiply(4), the output is 4 * 5 = 20.
❓ Predict Output
advanced2:00remaining
Lambda with incorrect syntax
Which option will cause a compilation error?
C Sharp (C#)
Func<int, int> f = x => x * 2;
Attempts:
2 left
💡 Hint
Check if the lambda body returns a value properly.
✗ Incorrect
Option C has a block body but no return statement, so it does not return a value, causing a compilation error.
🧠 Conceptual
expert2:00remaining
Understanding lambda expression type inference
Given the code below, what is the inferred delegate type of the lambda expression?
C Sharp (C#)
var list = new List<int> {1, 2, 3}; var result = list.Select(x => x.ToString());
Attempts:
2 left
💡 Hint
Select expects a function that takes an int and returns a string.
✗ Incorrect
The Select method expects a Func. Here TSource is int and TResult is string, so the lambda is Func.