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

Base class and derived class 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
🎖️
Inheritance Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of base and derived class method calls
What is the output of the following C# code?
C Sharp (C#)
using System;

class Animal {
    public virtual void Speak() {
        Console.WriteLine("Animal speaks");
    }
}

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

class Program {
    static void Main() {
        Animal a = new Animal();
        Animal b = new Dog();
        Dog d = new Dog();

        a.Speak();
        b.Speak();
        d.Speak();
    }
}
A
Animal speaks
Animal speaks
Dog barks
B
Animal speaks
Dog barks
Animal speaks
C
Animal speaks
Dog barks
Dog barks
D
Dog barks
Dog barks
Dog barks
Attempts:
2 left
💡 Hint
Remember that virtual methods allow derived classes to override behavior.
Predict Output
intermediate
2:00remaining
Output when base class constructor calls virtual method
What is the output of this C# program?
C Sharp (C#)
using System;

class Base {
    public Base() {
        Print();
    }
    public virtual void Print() {
        Console.WriteLine("Base Print");
    }
}

class Derived : Base {
    private int x = 5;
    public override void Print() {
        Console.WriteLine($"Derived Print: x = {x}");
    }
}

class Program {
    static void Main() {
        Derived d = new Derived();
    }
}
ADerived Print: x = 5
BDerived Print: x = 0
CBase Print
DCompilation error
Attempts:
2 left
💡 Hint
Think about the order of initialization when base constructor calls a virtual method.
🔧 Debug
advanced
2:00remaining
Why does this derived class method not override base method?
The following code compiles but does not produce the expected output. Why?
C Sharp (C#)
using System;

class Base {
    public void Show() {
        Console.WriteLine("Base Show");
    }
}

class Derived : Base {
    public void Show() {
        Console.WriteLine("Derived Show");
    }
}

class Program {
    static void Main() {
        Base b = new Derived();
        b.Show();
    }
}
ABecause the base class method is not marked virtual, so the derived method hides it but does not override.
BBecause the derived class method has a different name and signature.
CBecause the base class method is private and cannot be overridden.
DBecause the derived class method is static and cannot override instance methods.
Attempts:
2 left
💡 Hint
Check if the base method is virtual or not.
📝 Syntax
advanced
2:00remaining
Which option causes a compile-time error?
Which of the following code snippets will cause a compile-time error in C#?
Aclass Base { public virtual void Foo() {} } class Derived : Base { public override void Foo() {} }
Bclass Base { public virtual void Foo() {} } class Derived : Base { public void Foo() {} }
Cclass Base { public virtual void Foo() {} } class Derived : Base { public new void Foo() {} }
Dclass Base { public void Foo() {} } class Derived : Base { public override void Foo() {} }
Attempts:
2 left
💡 Hint
Override keyword requires the base method to be virtual or abstract.
🚀 Application
expert
2:00remaining
Number of accessible members in derived class instance
Given the classes below, how many members (methods and properties) can be accessed directly from an instance of Derived?
C Sharp (C#)
class Base {
    public void PublicMethod() {}
    protected void ProtectedMethod() {}
    private void PrivateMethod() {}
    public int PublicProperty { get; set; }
    protected int ProtectedProperty { get; set; }
    private int PrivateProperty { get; set; }
}

class Derived : Base {
    public void DerivedMethod() {}
    public int DerivedProperty { get; set; }
}

// In Main:
// Derived d = new Derived();
// How many members can be accessed via d. ?
A4
B5
C6
D7
Attempts:
2 left
💡 Hint
Remember that private members of base class are not accessible in derived class instances.

Practice

(1/5)
1. What is the main purpose of a base class in C#?
easy
A. To hold common code that other classes can reuse
B. To create objects directly without inheritance
C. To prevent other classes from inheriting
D. To store data only without any methods

Solution

  1. Step 1: Understand the role of base class

    A base class contains common code that multiple classes can share to avoid repetition.
  2. Step 2: Compare options with this role

    To hold common code that other classes can reuse matches this purpose exactly, while others describe incorrect or unrelated uses.
  3. Final Answer:

    To hold common code that other classes can reuse -> Option A
  4. Quick Check:

    Base class = shared code [OK]
Hint: Base class shares code for reuse [OK]
Common Mistakes:
  • Thinking base class cannot be instantiated
  • Confusing base class with interface
  • Believing base class only stores data
2. Which of the following is the correct syntax to declare a derived class Car that inherits from a base class Vehicle in C#?
easy
A. class Car inherits Vehicle { }
B. class Car : Vehicle { }
C. class Car extends Vehicle { }
D. class Car -> Vehicle { }

Solution

  1. Step 1: Recall C# inheritance syntax

    In C#, a derived class uses a colon (:) followed by the base class name.
  2. Step 2: Match options with correct syntax

    class Car : Vehicle { } uses the correct syntax: class Car : Vehicle { }. Others use incorrect keywords or symbols.
  3. Final Answer:

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

    Inheritance syntax = colon (:) [OK]
Hint: Use colon (:) to inherit in C# [OK]
Common Mistakes:
  • Using 'inherits' instead of ':'
  • Using 'extends' like in Java
  • Using arrow '->' symbol
3. What will be the output of this C# code?
class Animal {
    public void Speak() {
        Console.WriteLine("Animal speaks");
    }
}
class Dog : Animal {
    public void Speak() {
        Console.WriteLine("Dog barks");
    }
}

class Program {
    static void Main() {
        Animal a = new Dog();
        a.Speak();
    }
}
medium
A. Compilation error
B. Dog barks
C. Animal speaks
D. Runtime error

Solution

  1. Step 1: Understand method hiding vs overriding

    The Speak method in Dog hides the base method but is not marked virtual or override.
  2. Step 2: Check method call behavior

    Since a is of type Animal, it calls Animal.Speak() ignoring Dog's method.
  3. Final Answer:

    Animal speaks -> Option C
  4. Quick Check:

    Non-virtual method call = base method [OK]
Hint: Non-virtual methods call base version [OK]
Common Mistakes:
  • Assuming derived method runs without override
  • Confusing method hiding with overriding
  • Expecting polymorphism without virtual keyword
4. Identify the error in this code snippet:
class Person {
    public string Name;
}
class Student : Person {
    public string Name;
}

class Program {
    static void Main() {
        Student s = new Student();
        s.Name = "Alice";
        Console.WriteLine(s.Name);
    }
}
medium
A. No error, code runs and prints 'Alice'
B. Missing constructor in derived class
C. Derived class cannot declare a field with the same name as base class
D. Must use override keyword for Name field

Solution

  1. Step 1: Analyze the code execution

    The code declares a public field Name in both Person and Student, causing the derived field to hide the base one. This is allowed in C#.
  2. Step 2: Determine if there is an error

    The code compiles (with a compiler warning about hiding), executes successfully, and prints 'Alice' as it accesses the derived class's Name field.
  3. Final Answer:

    No error, code runs and prints 'Alice' -> Option A
  4. Quick Check:

    Field hiding allowed, no hard error [OK]
Hint: Field hiding is allowed but generates a warning [OK]
Common Mistakes:
  • Thinking derived class cannot declare same field name
  • Believing missing constructor causes issue
  • Mistaking fields for methods that need override
5. You want to create a base class Shape with a method Area() that derived classes Circle and Rectangle must implement differently. Which approach is best in C#?
hard
A. Use interface instead of base class for Area()
B. Make Area() a virtual method in Shape and override it in derived classes
C. Define Area() only in derived classes without base declaration
D. Declare Area() as an abstract method in Shape and implement in derived classes

Solution

  1. Step 1: Understand requirement for method implementation in derived classes

    The base class Shape should force derived classes to provide their own Area() implementation.
  2. Step 2: Choose correct C# feature

    Declaring Area() as abstract in Shape requires derived classes to implement it, matching the requirement.
  3. Final Answer:

    Declare Area() as an abstract method in Shape and implement in derived classes -> Option D
  4. Quick Check:

    Abstract method = must implement in derived [OK]
Hint: Use abstract method to force implementation [OK]
Common Mistakes:
  • Using virtual without override
  • Not declaring method in base class
  • Confusing interface with abstract class