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

Method overloading in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Method Overloading Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of overloaded methods with different parameter types

What is the output of this C# program?

C Sharp (C#)
using System;
class Program {
    static void Print(int x) {
        Console.WriteLine("Integer: " + x);
    }
    static void Print(string x) {
        Console.WriteLine("String: " + x);
    }
    static void Main() {
        Print(5);
        Print("hello");
    }
}
AInteger: 5\nInteger: hello
BInteger: 5\nString: hello
CString: 5\nInteger: hello
DString: 5\nString: hello
Attempts:
2 left
💡 Hint

Look at the method signatures and which one matches the argument type.

❓ Predict Output
intermediate
2:00remaining
Output when overloaded methods have default parameters

What will this program print?

C Sharp (C#)
using System;
class Program {
    static void Show(int x, int y = 10) {
        Console.WriteLine(x + y);
    }
    static void Show(int x) {
        Console.WriteLine(x * 2);
    }
    static void Main() {
        Show(5);
    }
}
A15
B10
CSyntax error
DRuntime error
Attempts:
2 left
💡 Hint

Check if these two methods can coexist with these signatures.

🔧 Debug
advanced
2:00remaining
Identify the error in overloaded methods with params keyword

Why does this code cause a compilation error?

C Sharp (C#)
using System;
class Program {
    static void Display(params int[] numbers) {
        Console.WriteLine("Params method");
    }
    static void Display(int x, int y) {
        Console.WriteLine("Two int method");
    }
    static void Main() {
        Display(1, 2);
    }
}
ANo error, output is "Two int method"
BNo error, output is "Params method"
CAmbiguous call error because both methods match the call Display(1, 2)
DRuntime error due to params array
Attempts:
2 left
💡 Hint

Think about how the compiler chooses between a params method and a fixed parameter method.

❓ Predict Output
advanced
2:00remaining
Output of overloaded methods with inheritance and type casting

What is the output of this program?

C Sharp (C#)
using System;
class Animal {}
class Dog : Animal {}
class Program {
    static void Speak(Animal a) {
        Console.WriteLine("Animal speaks");
    }
    static void Speak(Dog d) {
        Console.WriteLine("Dog barks");
    }
    static void Main() {
        Animal a = new Dog();
        Speak(a);
    }
}
AAnimal speaks
BDog barks
CRuntime error
DCompilation error
Attempts:
2 left
💡 Hint

Consider the static type of the variable used in the method call.

❓ Predict Output
expert
3:00remaining
Output of overloaded methods with ref and out parameters

What is the output of this program?

C Sharp (C#)
using System;
class Program {
    static void Modify(ref int x) {
        x = x + 10;
    }
    static void Modify(out int x) {
        x = 20;
    }
    static void Main() {
        int a = 5;
        Modify(ref a);
        Console.WriteLine(a);
        int b;
        Modify(out b);
        Console.WriteLine(b);
    }
}
ARuntime error due to uninitialized variable
B15\n0
CCompilation error due to ambiguous call
D15\n20
Attempts:
2 left
💡 Hint

Note how ref and out parameters differ and how they affect method selection.

Practice

(1/5)
1.

What is method overloading in C#?

easy
A. Changing the return type of a method without changing parameters
B. Using methods from a parent class in a child class
C. Having multiple methods with the same name but different parameters
D. Writing methods that call themselves recursively

Solution

  1. Step 1: Understand method naming

    Method overloading means using the same method name multiple times.
  2. Step 2: Check parameter differences

    Each overloaded method must have different parameters (type or count).
  3. Final Answer:

    Having multiple methods with the same name but different parameters -> Option C
  4. Quick Check:

    Method overloading = Same name, different parameters [OK]
Hint: Same method name but different parameters means overloading [OK]
Common Mistakes:
  • Confusing overloading with overriding
  • Thinking return type alone allows overloading
  • Believing parameter names must differ
2.

Which of the following is correct syntax for method overloading in C#?

public void Print(int x) { }
public void Print(string x) { }
easy
A. Both methods have the same name but different parameter types
B. Methods must have different names to overload
C. Return types must be different for overloading
D. Parameter names must be different for overloading

Solution

  1. Step 1: Check method names

    Both methods are named 'Print', which is allowed for overloading.
  2. Step 2: Check parameter types

    One method takes an int, the other a string, so parameters differ.
  3. Final Answer:

    Both methods have the same name but different parameter types -> Option A
  4. Quick Check:

    Same name + different parameters = valid overloading [OK]
Hint: Overloaded methods differ by parameter types or count, not names [OK]
Common Mistakes:
  • Thinking parameter names affect overloading
  • Believing return type difference is enough
  • Assuming method names must be unique
3.

What will be the output of this C# code?

class Program {
  static void Display(int x) { Console.WriteLine("Int: " + x); }
  static void Display(string x) { Console.WriteLine("String: " + x); }
  static void Main() {
    Display(5);
    Display("Hello");
  }
}
medium
A. Compilation error due to ambiguous call
B. Int: 5\nString: Hello
C. String: 5\nInt: Hello
D. Runtime error

Solution

  1. Step 1: Identify which Display method is called

    Display(5) calls Display(int x), printing "Int: 5".
  2. Step 2: Identify second method call

    Display("Hello") calls Display(string x), printing "String: Hello".
  3. Final Answer:

    Int: 5\nString: Hello -> Option B
  4. Quick Check:

    Overloaded methods called by matching parameter type [OK]
Hint: Calls match method by parameter type exactly [OK]
Common Mistakes:
  • Assuming ambiguous call error
  • Mixing output order
  • Expecting runtime error
4.

Find the error in this code snippet:

class Test {
  void Show(int x) { }
  void Show(int x) { }
}
medium
A. Return types differ so no error
B. No error, code compiles fine
C. Parameter names must differ for overloading
D. Methods have the same signature causing a compile error

Solution

  1. Step 1: Check method signatures

    Both methods are named 'Show' and take one int parameter, so signatures are identical.
  2. Step 2: Understand overloading rules

    Overloading requires different parameter types or counts; identical signatures cause errors.
  3. Final Answer:

    Methods have the same signature causing a compile error -> Option D
  4. Quick Check:

    Same name + same parameters = error [OK]
Hint: Overloaded methods must differ in parameters, not just names [OK]
Common Mistakes:
  • Thinking return type difference matters
  • Assuming parameter names affect overloading
  • Believing code compiles fine
5.

You want to create a class with a method Calculate that can add two integers, two doubles, or concatenate two strings. Which method signatures correctly implement method overloading?

hard
A. void Calculate(int a, int b), void Calculate(double a, double b), void Calculate(string a, string b)
B. int Calculate(int a, int b), int Calculate(int a, int b), string Calculate(string a, string b)
C. void Calculate(int a, int b), void Calculate(int a), void Calculate(string a, string b)
D. void Calculate(int a, int b), void Calculate(double a), void Calculate(string a)

Solution

  1. Step 1: Check parameter types and counts

    void Calculate(int a, int b), void Calculate(double a, double b), void Calculate(string a, string b) has three methods named Calculate with different parameter types but same count (2).
  2. Step 2: Verify overloading rules

    Different parameter types allow overloading; all methods differ by parameter type.
  3. Final Answer:

    void Calculate(int a, int b), void Calculate(double a, double b), void Calculate(string a, string b) -> Option A
  4. Quick Check:

    Same name, different parameter types = valid overloading [OK]
Hint: Overload by changing parameter types or count, not return type [OK]
Common Mistakes:
  • Using same parameters with different return types
  • Changing parameter count incorrectly
  • Confusing overloading with overriding
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