Challenge - 5 Problems
Abstract vs Concrete Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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()); } }
Attempts:
2 left
💡 Hint
Look at how the abstract method Speak is implemented in the Dog class.
✗ Incorrect
The abstract class Animal defines an abstract method Speak. The Dog class overrides Speak and returns "Woof!". The program creates a Dog instance and calls Speak, printing "Woof!".
🧠 Conceptual
intermediate2: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?
Attempts:
2 left
💡 Hint
Think about the purpose of abstract classes in design.
✗ Incorrect
Abstract classes are used to define a common base with some methods that subclasses must implement. Concrete classes can be instantiated directly.
🔧 Debug
advanced2: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(); } }
Attempts:
2 left
💡 Hint
Check if all abstract methods are implemented in subclasses.
✗ Incorrect
The Car class does not implement the abstract method Drive, so the compiler will raise an error.
📝 Syntax
advanced2:00remaining
Correct syntax for abstract method in C#
Which option shows the correct syntax to declare an abstract method in an abstract class?
Attempts:
2 left
💡 Hint
Abstract methods do not have a body and use the 'abstract' keyword before the return type.
✗ Incorrect
The correct syntax is 'public abstract void Run();' without a method body.
🚀 Application
expert3: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?
Attempts:
2 left
💡 Hint
Think about sharing code and forcing implementation of certain methods.
✗ Incorrect
An abstract class allows sharing common code (like logging) and forces subclasses to implement 'ProcessPayment'. Interfaces cannot have shared code.