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

When to use abstract vs concrete in C Sharp (C#) - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Abstract vs Concrete Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of abstract and concrete class usage
What is the output of this C# code when run?
C Sharp (C#)
abstract class Animal {
    public abstract string Speak();
}

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

class Program {
    static void Main() {
        Animal myDog = new Dog();
        System.Console.WriteLine(myDog.Speak());
    }
}
AWoof!
BAnimal
CCompile-time error: Cannot create instance of abstract class
DRuntime error: Method not implemented
Attempts:
2 left
💡 Hint
Look at how the abstract method Speak is implemented in the Dog class.
🧠 Conceptual
intermediate
2:00remaining
When to use abstract classes vs concrete classes
Which situation best describes when you should use an abstract class instead of a concrete class?
AWhen you want to provide a base class with some methods that must be implemented by subclasses
BWhen you want to create objects directly without any subclassing
CWhen you want to prevent any class from inheriting your class
DWhen you want to create a class with only static methods
Attempts:
2 left
💡 Hint
Think about the purpose of abstract classes in design.
🔧 Debug
advanced
2:00remaining
Identify the error in abstract class usage
What error will this code produce when compiled?
C Sharp (C#)
abstract class Vehicle {
    public abstract void Drive();
}

class Car : Vehicle {
    // Missing override of Drive method
}

class Program {
    static void Main() {
        Vehicle myCar = new Car();
        myCar.Drive();
    }
}
ANo error, outputs nothing
BRuntime error: Drive method not found
CCompile-time error: Car does not implement abstract method Drive
DCompile-time error: Cannot instantiate abstract class Vehicle
Attempts:
2 left
💡 Hint
Check if all abstract methods are implemented in subclasses.
📝 Syntax
advanced
2:00remaining
Correct syntax for abstract method in C#
Which option shows the correct syntax to declare an abstract method in an abstract class?
Apublic void abstract Run();
Bpublic abstract void Run() {}
Cabstract public void Run() {}
Dpublic abstract void Run();
Attempts:
2 left
💡 Hint
Abstract methods do not have a body and use the 'abstract' keyword before the return type.
🚀 Application
expert
3:00remaining
Choosing abstract vs concrete class design
You are designing a system for different types of payment methods. You want to ensure every payment method implements a 'ProcessPayment' method but also share some common code for logging. Which design is best?
ACreate a concrete class 'PaymentMethod' with all methods implemented and no inheritance
BCreate an abstract class 'PaymentMethod' with an abstract 'ProcessPayment' method and concrete logging methods
CCreate a concrete class 'PaymentMethod' with an empty 'ProcessPayment' method to override
DCreate an interface 'PaymentMethod' with 'ProcessPayment' and implement logging in each class
Attempts:
2 left
💡 Hint
Think about sharing code and forcing implementation of certain methods.