0
0
C Sharp (C#)programming~20 mins

Expression-bodied lambdas in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Expression-bodied Lambdas Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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));
A10
B25
C5
DCompilation error
Attempts:
2 left
💡 Hint
Remember that the lambda returns the result of x multiplied by itself.
Predict Output
intermediate
2: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"));
AHello, Anna!
BHello, {name}!
Cgreet(Anna)
DCompilation error
Attempts:
2 left
💡 Hint
The lambda uses string interpolation to insert the name.
🔧 Debug
advanced
2: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));
ACompilation error: Missing return statement
BOutputs 5
COutputs 3
DRuntime error: NullReferenceException
Attempts:
2 left
💡 Hint
Expression-bodied lambdas must return a value directly without braces or must use return inside braces.
Predict Output
advanced
2: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));
A7
BEven
CCompilation error
DOdd
Attempts:
2 left
💡 Hint
The lambda returns "Even" if the number is divisible by 2, otherwise "Odd".
Predict Output
expert
3: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));
A7
B3
C12
DCompilation error
Attempts:
2 left
💡 Hint
The outer lambda returns another lambda that multiplies two numbers.