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

Abstract classes and methods in C Sharp (C#) - Practice Problems & Coding Challenges

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 Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of abstract class method call
What is the output of this C# program?
C Sharp (C#)
using System;
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();
    }
}
AWoof
BAnimal speaks
CCompilation error: Cannot create instance of abstract class
DRuntime error: Method not implemented
Attempts:
2 left
💡 Hint
Remember that abstract classes cannot be instantiated directly, but you can create instances of derived classes.
🧠 Conceptual
intermediate
1:30remaining
Abstract method characteristics
Which statement about abstract methods in C# is correct?
AAbstract methods can have a body with implementation.
BAbstract methods can be static.
CAbstract methods must be declared in abstract classes.
DAbstract methods can be private.
Attempts:
2 left
💡 Hint
Think about where abstract methods are allowed and their purpose.
🔧 Debug
advanced
2:30remaining
Identify the compilation error
What is the cause of the compilation error in this code?
C Sharp (C#)
using System;
abstract class Shape {
    public abstract double Area();
}
class Circle : Shape {
    public double Radius;
}
class Program {
    static void Main() {
        Circle c = new Circle();
        c.Radius = 5;
        Console.WriteLine(c.Area());
    }
}
AArea method must be static
BCircle does not implement the abstract method Area()
CRadius field must be private
DCannot instantiate abstract class Shape
Attempts:
2 left
💡 Hint
Check if all abstract methods are implemented in derived classes.
📝 Syntax
advanced
2:00remaining
Correct syntax for abstract method override
Which option shows the correct way to override an abstract method in C#?
C Sharp (C#)
abstract class Vehicle {
    public abstract void Start();
}
class Car : Vehicle {
    // Choose the correct override syntax here
}
Apublic abstract override void Start() { Console.WriteLine("Car started"); }
Bpublic void Start() { Console.WriteLine("Car started"); }
Coverride public void Start() { Console.WriteLine("Car started"); }
Dpublic override void Start() { Console.WriteLine("Car started"); }
Attempts:
2 left
💡 Hint
Remember the order and keywords required for overriding methods.
🚀 Application
expert
3:00remaining
Number of methods to implement
Given the following code, how many methods must the class 'SmartPhone' implement to compile successfully?
C Sharp (C#)
abstract class Device {
    public abstract void PowerOn();
    public abstract void PowerOff();
}
abstract class Phone : Device {
    public abstract void Call(string number);
}
class SmartPhone : Phone {
    // Implementations here
}
A3
B2
C1
D4
Attempts:
2 left
💡 Hint
Count all abstract methods inherited from base classes that must be implemented.

Practice

(1/5)
1. What is true about an abstract class in C#?
easy
A. It can be instantiated like any other class.
B. It must have only abstract methods.
C. It cannot be instantiated directly.
D. It cannot have any methods.

Solution

  1. Step 1: Understand abstract class instantiation rules

    An abstract class is designed as a base template and cannot be created as an object directly.
  2. Step 2: Check other options for correctness

    Abstract classes can have both abstract and non-abstract methods, so options A, B, and D are incorrect.
  3. Final Answer:

    It cannot be instantiated directly. -> Option C
  4. Quick Check:

    Abstract class = no direct instantiation [OK]
Hint: Remember: abstract classes are blueprints, not objects. [OK]
Common Mistakes:
  • Thinking abstract classes can be instantiated.
  • Believing abstract classes must have only abstract methods.
  • Confusing abstract classes with interfaces.
2. Which of the following is the correct way to declare an abstract method in C#?
easy
A. public abstract void Display() {}
B. public abstract void Display();
C. abstract public void Display() {}
D. public void abstract Display() {}

Solution

  1. Step 1: Recall abstract method syntax

    Abstract methods have no body and end with a semicolon, declared with the 'abstract' keyword before the return type.
  2. Step 2: Validate each option

    public abstract void Display(); matches the correct syntax. public void abstract Display() {} and C have wrong keyword order or include a body. public abstract void Display() {} incorrectly includes a method body.
  3. Final Answer:

    public abstract void Display(); -> Option B
  4. Quick Check:

    Abstract method = declaration only, no body [OK]
Hint: Abstract methods end with semicolon, no braces. [OK]
Common Mistakes:
  • Adding method body to abstract methods.
  • Wrong keyword order in declaration.
  • Using braces {} with abstract methods.
3. What will be the output of the following code?
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());
    }
}
medium
A. Woof
B. Animal
C. Compile-time error
D. Runtime error

Solution

  1. Step 1: Understand class inheritance and method override

    Dog inherits from abstract Animal and implements the abstract Speak method returning "Woof".
  2. Step 2: Trace program execution

    Main creates a Dog object as Animal type and calls Speak(), which runs Dog's override returning "Woof".
  3. Final Answer:

    Woof -> Option A
  4. Quick Check:

    Override abstract method = Dog's Speak() output [OK]
Hint: Abstract method calls run subclass override. [OK]
Common Mistakes:
  • Expecting abstract class method output.
  • Thinking abstract classes can be instantiated.
  • Confusing compile-time and runtime errors.
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 class cannot inherit from Shape.
B. Area() method cannot return double.
C. Shape class cannot have abstract methods.
D. Circle must declare Area() as override.

Solution

  1. Step 1: Check method overriding rules

    When a subclass implements an abstract method, it must use the 'override' keyword.
  2. Step 2: Identify missing override keyword

    Circle's Area() method lacks 'override', causing a compile error.
  3. Final Answer:

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

    Override abstract method = must use 'override' keyword [OK]
Hint: Override abstract methods with 'override' keyword. [OK]
Common Mistakes:
  • Omitting 'override' keyword in subclass method.
  • Thinking abstract methods can be implemented without override.
  • Confusing return types.
5. You want to create a base class Vehicle with an abstract method StartEngine(). You also want to ensure every subclass implements StartEngine() differently. Which is the best approach?
hard
A. Make Vehicle an abstract class with an abstract StartEngine() method.
B. Make Vehicle a normal class and provide a default StartEngine() implementation.
C. Make Vehicle an interface with StartEngine() method.
D. Make Vehicle a sealed class with StartEngine() method.

Solution

  1. Step 1: Understand requirement for different implementations

    Each subclass must implement StartEngine() differently, so a base method without body is needed.
  2. Step 2: Choose correct class type and method declaration

    Abstract class Vehicle with abstract StartEngine() enforces subclasses to implement it uniquely.
  3. Final Answer:

    Make Vehicle an abstract class with an abstract StartEngine() method. -> Option A
  4. Quick Check:

    Abstract class + abstract method = enforced subclass implementation [OK]
Hint: Use abstract class + abstract method for enforced overrides. [OK]
Common Mistakes:
  • Using sealed class which prevents inheritance.
  • Using interface when base class behavior is needed.
  • Providing default method when unique implementations required.