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

Why interfaces are needed in C Sharp (C#) - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Interface Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Purpose of Interfaces in C#
Why do programmers use interfaces in C#?
ATo define a contract that classes can implement, ensuring they provide specific methods.
BTo replace inheritance completely and avoid code reuse.
CTo create objects directly without classes.
DTo store data like variables and constants only.
Attempts:
2 left
πŸ’‘ Hint
Think about how interfaces help different classes share common behavior.
❓ Predict Output
intermediate
2:00remaining
Output of Interface Implementation
What is the output of this C# code?
C Sharp (C#)
interface IAnimal {
    void Speak();
}

class Dog : IAnimal {
    public void Speak() {
        System.Console.WriteLine("Woof");
    }
}

class Cat : IAnimal {
    public void Speak() {
        System.Console.WriteLine("Meow");
    }
}

class Program {
    static void Main() {
        IAnimal animal = new Dog();
        animal.Speak();
        animal = new Cat();
        animal.Speak();
    }
}
A
Woof
Woof
B
Meow
Woof
C
Woof
Meow
DCompilation error because interfaces cannot be instantiated
Attempts:
2 left
πŸ’‘ Hint
Look at which class instance is assigned to the interface variable before calling Speak.
πŸ”§ Debug
advanced
2:00remaining
Identify the Error with Interface Implementation
What error will this code produce?
C Sharp (C#)
interface IVehicle {
    void Drive();
}

class Car : IVehicle {
    // Missing Drive method implementation
}

class Program {
    static void Main() {
        IVehicle v = new Car();
        v.Drive();
    }
}
ACompilation error: Cannot create instance of interface
BRuntime error: NullReferenceException
CNo error, outputs nothing
DCompilation error: 'Car' does not implement interface member 'IVehicle.Drive()'
Attempts:
2 left
πŸ’‘ Hint
Check if all interface methods are implemented in the class.
πŸ“ Syntax
advanced
2:00remaining
Correct Interface Syntax
Which option shows the correct way to declare an interface with one method in C#?
Ainterface IExample { void DoWork(); }
Binterface IExample { void DoWork() {} }
Cinterface IExample { void DoWork() => Console.WriteLine("Work"); }
Dinterface IExample { public void DoWork(); }
Attempts:
2 left
πŸ’‘ Hint
Interfaces only declare method signatures without bodies.
πŸš€ Application
expert
3:00remaining
Using Interfaces for Flexible Code
Given these classes and interface, what is the output of the program?
C Sharp (C#)
interface IShape {
    double Area();
}

class Circle : IShape {
    public double Radius { get; set; }
    public Circle(double r) { Radius = r; }
    public double Area() => 3.14 * Radius * Radius;
}

class Square : IShape {
    public double Side { get; set; }
    public Square(double s) { Side = s; }
    public double Area() => Side * Side;
}

class Program {
    static void Main() {
        IShape[] shapes = { new Circle(2), new Square(3) };
        double totalArea = 0;
        foreach (var shape in shapes) {
            totalArea += shape.Area();
        }
        System.Console.WriteLine($"Total area: {totalArea}");
    }
}
ATotal area: 13.56
BTotal area: 21.56
CTotal area: 18.56
DCompilation error due to interface usage
Attempts:
2 left
πŸ’‘ Hint
Calculate area of circle (Ο€rΒ²) and square (sideΒ²) and add them.