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

Interface vs abstract class decision in C Sharp (C#) - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Interface vs Abstract Class Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of interface and abstract class usage

What is the output of this C# code?

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

abstract class Animal : IAnimal {
    public abstract void Speak();
    public void Eat() {
        Console.WriteLine("Eating food");
    }
}

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

class Program {
    static void Main() {
        IAnimal animal = new Dog();
        animal.Speak();
        // animal.Eat(); // Uncommenting this line causes error
    }
}
AWoof
BEating food
CWoof\nEating food
DCompilation error
Attempts:
2 left
💡 Hint

Remember that interface references only expose interface members.

🧠 Conceptual
intermediate
1:30remaining
Choosing between interface and abstract class

You want to design a system where multiple unrelated classes share a method signature but have different implementations. Which is the best choice?

AUse an interface to declare the method signature
BUse an abstract class with implemented methods
CUse a sealed class
DUse a static class
Attempts:
2 left
💡 Hint

Think about multiple inheritance and unrelated classes.

Predict Output
advanced
2:00remaining
Output of abstract class with constructor and interface

What is the output of this C# program?

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

abstract class Vehicle : IVehicle {
    protected string model;
    public Vehicle(string model) {
        this.model = model;
    }
    public abstract void Start();
    public void ShowModel() {
        Console.WriteLine($"Model: {model}");
    }
}

class Car : Vehicle {
    public Car(string model) : base(model) {}
    public override void Start() {
        Console.WriteLine("Car started");
    }
}

class Program {
    static void Main() {
        IVehicle v = new Car("Sedan");
        v.Start();
        // v.ShowModel(); // Uncommenting causes error
    }
}
ACompilation error
BModel: Sedan
CCar started\nModel: Sedan
DCar started
Attempts:
2 left
💡 Hint

Interface reference only exposes interface methods.

🔧 Debug
advanced
2:30remaining
Identify the error in interface and abstract class usage

What error does this code produce?

C Sharp (C#)
interface IShape {
    double Area();
}

abstract class Shape : IShape {
    public virtual double Area() {
        return 0;
    }
}

class Circle : Shape {
    private double radius;
    public Circle(double r) {
        radius = r;
    }
    public override double Area() {
        return Math.PI * radius * radius;
    }
}

class Program {
    static void Main() {
        IShape s = new Circle(3);
        Console.WriteLine(s.Area());
    }
}
ARuntime error: NullReferenceException
BOutput: 28.2743338823081
CCompilation error: missing interface implementation
DCompilation error: cannot override non-virtual method
Attempts:
2 left
💡 Hint

Check method modifiers in abstract class and override usage.

🧠 Conceptual
expert
3:00remaining
When to use abstract class over interface

Which scenario best justifies using an abstract class instead of an interface in C#?

AYou want to declare only method signatures without any implementation.
BYou want to allow multiple inheritance of behavior from unrelated classes.
CYou want to provide default method implementations and maintain state for derived classes.
DYou want to create a contract that unrelated classes must follow.
Attempts:
2 left
💡 Hint

Think about code reuse and shared data.