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

Default interface methods in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Default Interface Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of default interface method call

What is the output of this C# code?

C Sharp (C#)
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();
    }
}
ACompile-time error: 'Person' does not implement 'SayHello()'
BHello from interface
CRuntime error: Method not found
DNo output
Attempts:
2 left
💡 Hint

Check if the class implements the interface method explicitly or uses the default.

Predict Output
intermediate
2:00remaining
Overriding default interface method

What will this program print?

C Sharp (C#)
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();
    }
}
ABark
BRuntime error
CCompile-time error: 'Dog' must implement 'Speak()'
DAnimal sound
Attempts:
2 left
💡 Hint

Does the class Dog provide its own Speak() method?

🔧 Debug
advanced
2:00remaining
Why does this code cause a compile error?

Examine the code below. Why does it cause a compile-time error?

C Sharp (C#)
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);
    }
}
ANo error, output is 5
BCompile error because default interface methods are not supported in C#
CCompile error because Calculator does not implement Add explicitly and cannot use default interface method
DRuntime error because Add method is missing
Attempts:
2 left
💡 Hint

Check how default interface methods are accessed through class instances.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in default interface method

Which option contains a syntax error in defining a default interface method?

Ainterface ITest { void Show() => Console.WriteLine("Hi"); }
Binterface ITest { void Show() { Console.WriteLine("Hi"); } }
Cinterface ITest { void Show(); }
Dinterface ITest { void Show() => { Console.WriteLine("Hi"); }; }
Attempts:
2 left
💡 Hint

Check the syntax for expression-bodied members in interfaces.

🚀 Application
expert
2:00remaining
How many methods must a class implement for this interface?

Consider this interface with default methods and one abstract method. How many methods must a class implement if it implements this interface?

C Sharp (C#)
interface IOperations
{
    void Start();
    void Stop() => Console.WriteLine("Stopping");
    int Status() => 1;
}
AOne method: only Start() must be implemented
BThree methods: Start(), Stop(), and Status() must be implemented
CTwo methods: Start() and Stop() must be implemented
DNo methods need to be implemented
Attempts:
2 left
💡 Hint

Default interface methods provide implementation. Abstract methods must be implemented.