Bird
Raised Fist0
C Sharp (C#)programming~20 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. Which statement best describes when to use an abstract class in C#?
easy
A. When you want to define a common plan without providing full implementation.
B. When you want to create a fully working class that can be instantiated.
C. When you want to prevent inheritance altogether.
D. When you want to create a class with only static methods.

Solution

  1. Step 1: Understand abstract class purpose

    An abstract class defines methods or properties that must be implemented by subclasses but does not provide full implementation itself.
  2. Step 2: Compare with concrete class

    Concrete classes provide full working code and can be instantiated, unlike abstract classes.
  3. Final Answer:

    When you want to define a common plan without providing full implementation. -> Option A
  4. Quick Check:

    Abstract class = common plan without full code [OK]
Hint: Abstract = plan only, Concrete = full working code [OK]
Common Mistakes:
  • Confusing abstract with concrete classes
  • Thinking abstract classes can be instantiated
  • Believing abstract classes provide full method bodies
2. Which of the following is the correct way to declare an abstract class in C#?
easy
A. class abstract Vehicle { }
B. public abstract class Vehicle { }
C. abstract public Vehicle { }
D. public class abstract Vehicle { }

Solution

  1. Step 1: Recall C# syntax for abstract classes

    The correct syntax places the keyword abstract before class and then the class name.
  2. Step 2: Check each option

    public abstract class Vehicle { } uses public abstract class Vehicle { }, which is correct. Other options have incorrect keyword order or missing keywords.
  3. Final Answer:

    public abstract class Vehicle { } -> Option B
  4. Quick Check:

    abstract class syntax = 'public abstract class' [OK]
Hint: Use 'abstract' before 'class' keyword [OK]
Common Mistakes:
  • Placing 'abstract' after 'class'
  • Omitting 'class' keyword
  • Incorrect keyword order
3. What will be the output of this C# code?
abstract class Animal {
    public abstract void Speak();
}

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

class Program {
    static void Main() {
        Animal a = new Dog();
        a.Speak();
    }
}
medium
A. Runtime error because Speak is abstract
B. Compile-time error because Animal is abstract
C. Woof
D. No output

Solution

  1. Step 1: Understand abstract method implementation

    The abstract method Speak in Animal is overridden in Dog with a concrete implementation that prints "Woof".
  2. Step 2: Analyze runtime behavior

    Creating an Animal reference to a Dog object and calling Speak() calls the overridden method, printing "Woof".
  3. Final Answer:

    Woof -> Option C
  4. Quick Check:

    Abstract method overridden = prints 'Woof' [OK]
Hint: Abstract method must be overridden to run [OK]
Common Mistakes:
  • Thinking abstract class cannot be referenced
  • Expecting compile or runtime error
  • Assuming abstract methods have bodies
4. Identify the error in this code snippet:
abstract class Shape {
    public abstract double Area();
}

class Circle : Shape {
    public double Area() {
        return 3.14 * 5 * 5;
    }
}
medium
A. Circle must declare Area() as override
B. Shape cannot have abstract methods
C. Circle cannot inherit from Shape
D. Area method should return int, not double

Solution

  1. Step 1: Check abstract method override rules

    When a class inherits an abstract method, it must override it using the override keyword.
  2. Step 2: Identify missing override keyword

    The Circle class defines Area() but misses override, causing a compile error.
  3. Final Answer:

    Circle must declare Area() as override -> Option A
  4. Quick Check:

    Override keyword required for abstract methods [OK]
Hint: Override abstract methods with 'override' keyword [OK]
Common Mistakes:
  • Omitting override keyword
  • Thinking abstract methods can be ignored
  • Confusing return types
5. You want to design a system where all vehicles must have a method StartEngine(), but the way engines start differs by vehicle type. Which approach is best in C#?
hard
A. Create a static class Vehicle with static StartEngine() method.
B. Create a concrete class Vehicle with a fully implemented StartEngine() method for all vehicles.
C. Use an interface with a concrete StartEngine() method and inherit it in all vehicle classes.
D. Create an abstract class Vehicle with abstract method StartEngine(), then implement it in subclasses.

Solution

  1. Step 1: Analyze requirement for different implementations

    Since StartEngine() differs by vehicle type, it should be declared abstract to force subclasses to provide their own version.
  2. Step 2: Choose correct class design

    An abstract class Vehicle with an abstract StartEngine() method fits best, allowing subclasses to implement specific behavior.
  3. Final Answer:

    Create an abstract class Vehicle with abstract method StartEngine(), then implement it in subclasses. -> Option D
  4. Quick Check:

    Abstract class for common plan, concrete for specifics [OK]
Hint: Abstract method for varying behavior, concrete class for details [OK]
Common Mistakes:
  • Using concrete class with one method for all vehicles
  • Trying to put method body in interface (not allowed)
  • Using static class which can't be inherited