What is the output of this C# code?
using System; interface IGreet { void SayHello() => Console.WriteLine("Hello from interface"); } class Person : IGreet { } class Program { static void Main() { IGreet p = new Person(); p.SayHello(); } }
Check if the class implements the interface method explicitly or uses the default.
The interface IGreet has a default implementation for SayHello(). The class Person does not override it, so calling SayHello() on an IGreet reference calls the default method, printing "Hello from interface".
What will this program print?
using System; interface IAnimal { void Speak() => Console.WriteLine("Animal sound"); } class Dog : IAnimal { public void Speak() => Console.WriteLine("Bark"); } class Program { static void Main() { IAnimal a = new Dog(); a.Speak(); } }
Does the class Dog provide its own Speak() method?
Dog overrides the default Speak() method from IAnimal. So calling Speak() on an IAnimal reference to a Dog instance calls Dog's Speak(), printing "Bark".
Examine the code below. Why does it cause a compile-time error?
interface ICalc { int Add(int a, int b) => a + b; } class Calculator : ICalc { // No Add method implemented here } class Program { static void Main() { Calculator calc = new Calculator(); int result = calc.Add(2, 3); System.Console.WriteLine(result); } }
Check how default interface methods are accessed through class instances.
Default interface methods can only be called through an interface reference. Calculator class instance cannot call Add() directly because it does not implement Add explicitly. So this causes a compile error.
Which option contains a syntax error in defining a default interface method?
Check the syntax for expression-bodied members in interfaces.
Option D incorrectly uses braces with expression-bodied syntax. Expression-bodied members use => followed by an expression without braces. Option D is valid because default interface methods support block bodies.
Consider this interface with default methods and one abstract method. How many methods must a class implement if it implements this interface?
interface IOperations { void Start(); void Stop() => Console.WriteLine("Stopping"); int Status() => 1; }
Default interface methods provide implementation. Abstract methods must be implemented.
Only Start() is abstract and must be implemented by the class. Stop() and Status() have default implementations, so the class can use them as is.