Challenge - 5 Problems
Expression-bodied Methods Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of expression-bodied method with arithmetic
What is the output of this C# code using an expression-bodied method?
C Sharp (C#)
class Calculator { public int Double(int x) => x * 2; } var calc = new Calculator(); System.Console.WriteLine(calc.Double(5));
Attempts:
2 left
💡 Hint
Look at how the method returns the value directly using the expression-bodied syntax.
✗ Incorrect
The method Double returns x multiplied by 2. For input 5, output is 10.
❓ Predict Output
intermediate2:00remaining
Expression-bodied property output
What will be printed by this C# program using an expression-bodied property?
C Sharp (C#)
class Person { public string Name { get; } public Person(string name) => Name = name; public string Greeting => $"Hello, {Name}!"; } var p = new Person("Anna"); System.Console.WriteLine(p.Greeting);
Attempts:
2 left
💡 Hint
The Greeting property uses expression-bodied syntax to return a formatted string.
✗ Incorrect
The Greeting property returns "Hello, " plus the Name property value, which is "Anna".
❓ Predict Output
advanced2:00remaining
Expression-bodied method with conditional expression
What is the output of this C# code using an expression-bodied method with a conditional operator?
C Sharp (C#)
class NumberCheck { public string Check(int n) => n % 2 == 0 ? "Even" : "Odd"; } var checker = new NumberCheck(); System.Console.WriteLine(checker.Check(7));
Attempts:
2 left
💡 Hint
The method returns "Even" if the number is divisible by 2, otherwise "Odd".
✗ Incorrect
7 is not divisible by 2, so the method returns "Odd".
❓ Predict Output
advanced2:00remaining
Expression-bodied method with tuple return
What is the output of this C# code that uses an expression-bodied method returning a tuple?
C Sharp (C#)
class Point { public int X { get; } public int Y { get; } public Point(int x, int y) => (X, Y) = (x, y); public (int, int) GetCoordinates() => (X, Y); } var pt = new Point(3, 4); var coords = pt.GetCoordinates(); System.Console.WriteLine($"({coords.Item1}, {coords.Item2})");
Attempts:
2 left
💡 Hint
The method returns the tuple of X and Y properties directly.
✗ Incorrect
The GetCoordinates method returns (3, 4), so the output is (3, 4).
❓ Predict Output
expert2:00remaining
Expression-bodied method with pattern matching and switch expression
What is the output of this C# code using an expression-bodied method with a switch expression and pattern matching?
C Sharp (C#)
class Shape { public string Type { get; } public double Size { get; } public Shape(string type, double size) => (Type, Size) = (type, size); public string Describe() => Type switch { "Circle" when Size > 0 => $"Circle with radius {Size}", "Square" when Size > 0 => $"Square with side {Size}", _ => "Unknown shape" }; } var shape = new Shape("Circle", 5); System.Console.WriteLine(shape.Describe());
Attempts:
2 left
💡 Hint
The switch expression matches the Type and checks the Size condition.
✗ Incorrect
The Type is "Circle" and Size is 5 > 0, so it returns "Circle with radius 5".