Challenge - 5 Problems
Expression-bodied Lambdas Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of an expression-bodied lambda with arithmetic
What is the output of this C# code using an expression-bodied lambda?
C Sharp (C#)
Func<int, int> square = x => x * x; Console.WriteLine(square(5));
Attempts:
2 left
💡 Hint
Remember that the lambda returns the result of x multiplied by itself.
✗ Incorrect
The lambda takes an integer x and returns x * x. For input 5, the output is 25.
❓ Predict Output
intermediate2:00remaining
Output of expression-bodied lambda with string interpolation
What does this expression-bodied lambda print?
C Sharp (C#)
Func<string, string> greet = name => $"Hello, {name}!"; Console.WriteLine(greet("Anna"));
Attempts:
2 left
💡 Hint
The lambda uses string interpolation to insert the name.
✗ Incorrect
The lambda returns the string with the name inserted. For "Anna", it prints "Hello, Anna!".
🔧 Debug
advanced2:00remaining
Identify the error in this expression-bodied lambda
What error does this code produce?
C Sharp (C#)
Func<int, int> add = x => { x + 2; }; Console.WriteLine(add(3));
Attempts:
2 left
💡 Hint
Expression-bodied lambdas must return a value directly without braces or must use return inside braces.
✗ Incorrect
The lambda uses braces but does not have a return statement, causing a compilation error.
❓ Predict Output
advanced2:00remaining
Output of expression-bodied lambda with conditional operator
What is the output of this code?
C Sharp (C#)
Func<int, string> checkEven = n => n % 2 == 0 ? "Even" : "Odd"; Console.WriteLine(checkEven(7));
Attempts:
2 left
💡 Hint
The lambda returns "Even" if the number is divisible by 2, otherwise "Odd".
✗ Incorrect
7 is not divisible by 2, so the lambda returns "Odd".
❓ Predict Output
expert3:00remaining
Output of nested expression-bodied lambdas
What is the output of this nested expression-bodied lambda code?
C Sharp (C#)
Func<int, Func<int, int>> multiplier = x => y => x * y; var times3 = multiplier(3); Console.WriteLine(times3(4));
Attempts:
2 left
💡 Hint
The outer lambda returns another lambda that multiplies two numbers.
✗ Incorrect
multiplier(3) returns a lambda that multiplies its input by 3. times3(4) returns 3 * 4 = 12.