What is the output of this C# code that uses an interface as a contract?
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()); } }
Remember, the interface defines a method contract that the class must implement.
The class Dog implements the IAnimal interface by providing the Speak method. The variable pet is of type IAnimal but holds a Dog instance. Calling Speak() on pet calls Dog's implementation, which returns "Woof!".
Which statement best describes the role of an interface in C#?
Think about what an interface requires from classes that use it.
Interfaces in C# declare method and property signatures without implementation. Classes that implement the interface must provide those implementations, thus following the contract. Interfaces cannot hold state or provide default implementations (except default interface methods in newer versions, but that is advanced and not the main contract idea).
Given the code below, why does it fail to compile?
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(); } }
Check method names and case sensitivity carefully.
C# is case-sensitive. The interface requires a method named Drive with uppercase D. The class Car defines drive with lowercase d, which is a different method. Therefore, Car does not implement the interface method, causing a compile error.
Which option shows the correct way to implement this interface in C#?
interface IShape { double Area(); }
Remember the correct keyword for implementing interfaces and method signatures.
Option D uses the correct syntax: class Circle : IShape to implement the interface and declares Area as a public method returning double. Option D uses Java syntax implements which is invalid in C#. Option D misses the public modifier, causing a compile error because interface methods must be public. Option D has wrong return type void instead of double.
Consider this interface and two classes implementing it. What is the output when running the program?
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"); } }
Think about how the interface variable can hold different implementations.
The variable logger is of type ILogger. First, it holds a ConsoleLogger instance, so Log("Start") prints "Console: Start". Then it is assigned a FileLogger instance, so Log("End") prints "File: End". This shows how interfaces act as contracts allowing different implementations to be used interchangeably.