Bird
Raised Fist0
C Sharp (C#)programming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using expression-bodied methods in C#?
easy
A. To handle exceptions more efficiently
B. To declare variables with less code
C. To create classes without constructors
D. To write simple methods in a shorter and clearer way using the => syntax

Solution

  1. Step 1: Understand expression-bodied methods

    Expression-bodied methods use the => symbol to return a single expression directly, making code shorter.
  2. Step 2: Identify the purpose

    The main purpose is to simplify simple methods by writing them in one line with clear syntax.
  3. Final Answer:

    To write simple methods in a shorter and clearer way using the => syntax -> Option D
  4. Quick Check:

    Expression-bodied methods = shorter, clearer methods [OK]
Hint: Expression-bodied methods use => for short, single-expression methods [OK]
Common Mistakes:
  • Confusing expression-bodied methods with variable declarations
  • Thinking they handle exceptions
  • Believing they create classes
2. Which of the following is the correct syntax for an expression-bodied method that returns the square of an integer n?
easy
A. public int Square(int n) => n * n;
B. public int Square(int n) { return n * n; }
C. public int Square(int n) => { return n * n; };
D. public int Square(int n) => return n * n;

Solution

  1. Step 1: Recall expression-bodied method syntax

    Expression-bodied methods use => followed directly by the expression without braces or return keyword.
  2. Step 2: Check each option

    public int Square(int n) => n * n; correctly uses => n * n; without braces or return. public int Square(int n) { return n * n; } uses normal method syntax. Options C and D have syntax errors.
  3. Final Answer:

    public int Square(int n) => n * n; -> Option A
  4. Quick Check:

    Correct syntax = public int Square(int n) => n * n; [OK]
Hint: Use => expression; without braces or return for expression-bodied methods [OK]
Common Mistakes:
  • Adding braces {} around the expression
  • Using return keyword with =>
  • Missing semicolon at the end
3. What will be the output of the following C# code?
class Program {
    static int Double(int x) => x * 2;
    static void Main() {
        Console.WriteLine(Double(5));
    }
}
medium
A. 5
B. 10
C. Syntax error
D. 0

Solution

  1. Step 1: Understand the method Double

    The method Double takes an integer x and returns x multiplied by 2 using expression-bodied syntax.
  2. Step 2: Calculate the output for Double(5)

    Double(5) returns 5 * 2 = 10, which is printed by Console.WriteLine.
  3. Final Answer:

    10 -> Option B
  4. Quick Check:

    Double(5) = 10 [OK]
Hint: Multiply input by 2 in expression-bodied method to get output [OK]
Common Mistakes:
  • Confusing output with input value
  • Expecting syntax error due to unfamiliar syntax
  • Ignoring return value
4. Identify the error in the following expression-bodied method and choose the correct fix:
public int Add(int a, int b) => { return a + b; };
medium
A. Change => to =>> : public int Add(int a, int b) =>> a + b;
B. Add semicolon after return: public int Add(int a, int b) => { return a + b; };
C. Remove braces and return keyword: public int Add(int a, int b) => a + b;
D. No error, code is correct

Solution

  1. Step 1: Analyze expression-bodied method syntax

    Expression-bodied methods must have a single expression after => without braces or return keyword.
  2. Step 2: Identify the fix

    Remove braces and return keyword to fix syntax: public int Add(int a, int b) => a + b;
  3. Final Answer:

    Remove braces and return keyword: public int Add(int a, int b) => a + b; -> Option C
  4. Quick Check:

    Expression-bodied method = single expression after => [OK]
Hint: No braces or return with =>; just expression [OK]
Common Mistakes:
  • Using braces {} with expression-bodied methods
  • Including return keyword after =>
  • Thinking =>> is valid syntax
5. You want to create an expression-bodied method IsEven that returns true if an integer is even, and false otherwise. Which of the following implementations is correct?
hard
A. public bool IsEven(int n) => n % 2 == 0;
B. public bool IsEven(int n) => { if (n % 2 == 0) return true; else return false; };
C. public bool IsEven(int n) => return n % 2 == 0;
D. public bool IsEven(int n) => n / 2 == 0;

Solution

  1. Step 1: Understand the method goal

    The method should return true if n is even (n % 2 == 0), false otherwise.
  2. Step 2: Check each option

    public bool IsEven(int n) => n % 2 == 0; correctly uses expression-bodied syntax with the expression n % 2 == 0. public bool IsEven(int n) => { if (n % 2 == 0) return true; else return false; }; uses braces and return, which is invalid for expression-bodied methods. public bool IsEven(int n) => return n % 2 == 0; incorrectly uses return keyword after =>. public bool IsEven(int n) => n / 2 == 0; incorrectly checks n / 2 == 0, which is wrong logic.
  3. Final Answer:

    public bool IsEven(int n) => n % 2 == 0; -> Option A
  4. Quick Check:

    IsEven uses n % 2 == 0 with => [OK]
Hint: Use expression n % 2 == 0 directly after => for boolean return [OK]
Common Mistakes:
  • Using braces and return in expression-bodied methods
  • Adding return keyword after =>
  • Incorrect logic like n / 2 == 0
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard