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

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

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