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

Interface as contract mental model in C Sharp (C#) - Practice Problems & Coding Challenges

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!
Predict Output
intermediate
2:00remaining
Output of interface method call

What is the output of this C# code that uses an interface as a contract?

C Sharp (C#)
interface IAnimal {
    string Speak();
}

class Dog : IAnimal {
    public string Speak() {
        return "Woof!";
    }
}

class Program {
    static void Main() {
        IAnimal pet = new Dog();
        System.Console.WriteLine(pet.Speak());
    }
}
AWoof! Woof!
BWoof!
CCompile-time error: Dog does not implement IAnimal
DRuntime error: Cannot convert Dog to IAnimal
Attempts:
2 left
💡 Hint

Remember, the interface defines a method contract that the class must implement.

🧠 Conceptual
intermediate
1:30remaining
Interface contract enforcement

Which statement best describes the role of an interface in C#?

AAn interface defines a contract that classes must follow by implementing its methods and properties.
BAn interface can hold state and fields to share data between classes.
CAn interface provides default implementations for all its methods.
DAn interface is a class that can be instantiated directly.
Attempts:
2 left
💡 Hint

Think about what an interface requires from classes that use it.

🔧 Debug
advanced
2:30remaining
Why does this interface implementation fail?

Given the code below, why does it fail to compile?

C Sharp (C#)
interface IVehicle {
    void Drive();
}

class Car : IVehicle {
    public void drive() {
        System.Console.WriteLine("Driving");
    }
}

class Program {
    static void Main() {
        IVehicle v = new Car();
        v.Drive();
    }
}
AMethod name in Car is lowercase 'drive' instead of 'Drive', so it does not implement the interface method.
BInterface IVehicle cannot be implemented by a class named Car.
CThe interface method Drive must return a value, but Car's method returns void.
DThe class Car must be declared abstract to implement an interface.
Attempts:
2 left
💡 Hint

Check method names and case sensitivity carefully.

📝 Syntax
advanced
2:00remaining
Correct interface implementation syntax

Which option shows the correct way to implement this interface in C#?

C Sharp (C#)
interface IShape {
    double Area();
}
A
class Circle : IShape {
    public void Area() {
        return 3.14 * 2 * 2;
    }
}
B
class Circle implements IShape {
    public double Area() {
        return 3.14 * 2 * 2;
    }
}
C
class Circle : IShape {
    double Area() {
        return 3.14 * 2 * 2;
    }
}
D
class Circle : IShape {
    public double Area() {
        return 3.14 * 2 * 2;
    }
}
Attempts:
2 left
💡 Hint

Remember the correct keyword for implementing interfaces and method signatures.

🚀 Application
expert
3:00remaining
Interface contract with multiple implementations

Consider this interface and two classes implementing it. What is the output when running the program?

C Sharp (C#)
interface ILogger {
    void Log(string message);
}

class ConsoleLogger : ILogger {
    public void Log(string message) {
        System.Console.WriteLine($"Console: {message}");
    }
}

class FileLogger : ILogger {
    public void Log(string message) {
        System.Console.WriteLine($"File: {message}");
    }
}

class Program {
    static void Main() {
        ILogger logger = new ConsoleLogger();
        logger.Log("Start");
        logger = new FileLogger();
        logger.Log("End");
    }
}
A
Start
End
B
File: Start
Console: End
C
Console: Start
File: End
DCompile-time error: Cannot assign FileLogger to ILogger
Attempts:
2 left
💡 Hint

Think about how the interface variable can hold different implementations.