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

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Expression-bodied Methods Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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));
A25
B5
C10
DCompilation error
Attempts:
2 left
💡 Hint
Look at how the method returns the value directly using the expression-bodied syntax.
Predict Output
intermediate
2: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);
AHello, Anna!
BHello, !
CAnna
DCompilation error
Attempts:
2 left
💡 Hint
The Greeting property uses expression-bodied syntax to return a formatted string.
Predict Output
advanced
2: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));
A7
BOdd
CEven
DCompilation error
Attempts:
2 left
💡 Hint
The method returns "Even" if the number is divisible by 2, otherwise "Odd".
Predict Output
advanced
2: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})");
ACompilation error
B(4, 3)
C(0, 0)
D(3, 4)
Attempts:
2 left
💡 Hint
The method returns the tuple of X and Y properties directly.
Predict Output
expert
2: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());
ACircle with radius 5
BSquare with side 5
CUnknown shape
DCompilation error
Attempts:
2 left
💡 Hint
The switch expression matches the Type and checks the Size condition.