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

Why inheritance is needed in C Sharp (C#) - Test Your Understanding

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
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a class Dog that inherits from Animal.

C Sharp (C#)
class Dog : [1] { }
Drag options to blanks, or click blank then click option'
ADog
BBase
CAnimal
DObject
Attempts:
3 left
💡 Hint
Common Mistakes
Using the child class name instead of the parent class name.
Using a keyword like 'Base' which is not a class name.
2fill in blank
medium

Complete the code to call the base class constructor from the derived class Car.

C Sharp (C#)
class Car : Vehicle {
    public Car() : [1]() { }
}
Drag options to blanks, or click blank then click option'
Abase
Bthis
Csuper
Dparent
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'this' instead of 'base' to call the base constructor.
Using 'super' which is not a C# keyword.
3fill in blank
hard

Fix the error in the code to override the Speak method in the derived class Cat.

C Sharp (C#)
class Cat : Animal {
    public override void [1]() {
        Console.WriteLine("Meow");
    }
}
Drag options to blanks, or click blank then click option'
Aspeak
BSpeak
CSay
DTalk
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase 'speak' instead of 'Speak'.
Using a different method name like 'Say' or 'Talk'.
4fill in blank
hard

Fill both blanks to create a derived class Bird that inherits from Animal and overrides the Move method.

C Sharp (C#)
class Bird : [1] {
    public override void [2]() {
        Console.WriteLine("Flying");
    }
}
Drag options to blanks, or click blank then click option'
AAnimal
BMove
CFly
DSpeak
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong base class name.
Overriding a method with a wrong name.
5fill in blank
hard

Fill all three blanks to create a dictionary that maps animal names to their sounds using inheritance concepts.

C Sharp (C#)
var sounds = new Dictionary<string, string> {
    { [1], [2] },
    { [3], "Bark" }
};
Drag options to blanks, or click blank then click option'
A"Cat"
B"Meow"
C"Dog"
D"Purr"
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping keys and values.
Using incorrect string quotes.

Practice

(1/5)
1. Why do we use inheritance in C# programming?
easy
A. To make programs run faster by skipping code
B. To create unrelated classes with no shared features
C. To reuse code from an existing class in a new class
D. To avoid writing any methods in classes

Solution

  1. Step 1: Understand inheritance purpose

    Inheritance allows a new class to get properties and methods from an existing class, so we don't rewrite code.
  2. Step 2: Compare options

    Only To reuse code from an existing class in a new class correctly describes code reuse through inheritance. Others describe unrelated or incorrect uses.
  3. Final Answer:

    To reuse code from an existing class in a new class -> Option C
  4. Quick Check:

    Inheritance = Code reuse [OK]
Hint: Inheritance means new class gets old class features [OK]
Common Mistakes:
  • Thinking inheritance makes code run faster
  • Believing inheritance creates unrelated classes
  • Assuming inheritance removes need for methods
2. Which of the following is the correct syntax to inherit class Animal in C#?
easy
A. class Dog inherits Animal { }
B. class Dog : Animal { }
C. class Dog extends Animal { }
D. class Dog -> Animal { }

Solution

  1. Step 1: Recall C# inheritance syntax

    In C#, a class inherits another using a colon (:), like class Child : Parent { }.
  2. Step 2: Check each option

    class Dog : Animal { } uses the correct colon syntax. The other options use incorrect keywords or symbols.
  3. Final Answer:

    class Dog : Animal { } -> Option B
  4. Quick Check:

    Inheritance syntax in C# uses ':' [OK]
Hint: Use ':' to inherit a class in C# [OK]
Common Mistakes:
  • Using 'inherits' instead of ':'
  • Using 'extends' like in Java
  • Using arrows or other symbols
3. What will be the output of this C# code?
class Animal { public void Speak() { Console.WriteLine("Animal speaks"); } }
class Dog : Animal { public void Bark() { Console.WriteLine("Dog barks"); } }
var d = new Dog();
d.Speak();
medium
A. Dog barks
B. No output
C. Compile error
D. Animal speaks

Solution

  1. Step 1: Understand inheritance and method calls

    Dog inherits Animal, so Dog objects can call Animal's methods like Speak().
  2. Step 2: Analyze the code output

    Calling d.Speak() runs Animal's Speak method, printing "Animal speaks".
  3. Final Answer:

    Animal speaks -> Option D
  4. Quick Check:

    Inherited method runs = "Animal speaks" [OK]
Hint: Inherited methods can be called on child objects [OK]
Common Mistakes:
  • Thinking Bark() runs instead of Speak()
  • Expecting compile error due to inheritance
  • Assuming no output without calling Bark()
4. Identify the error in this inheritance code:
class Vehicle { public void Move() { Console.WriteLine("Moving"); } }
class Car Vehicle { public void Honk() { Console.WriteLine("Honk!"); } }
medium
A. Missing colon ':' between Car and Vehicle
B. Method Move() should be abstract
C. Car class cannot have methods
D. Vehicle class must be sealed

Solution

  1. Step 1: Check inheritance syntax

    In C#, inheritance requires a colon ':' between child and parent class names.
  2. Step 2: Locate the syntax error

    The code uses 'class Car Vehicle' missing the colon, causing a syntax error.
  3. Final Answer:

    Missing colon ':' between Car and Vehicle -> Option A
  4. Quick Check:

    Inheritance needs ':' separator [OK]
Hint: Remember ':' after child class name for inheritance [OK]
Common Mistakes:
  • Omitting ':' in inheritance
  • Thinking methods must be abstract
  • Believing parent class must be sealed
5. You want to create a class ElectricCar that has all features of Car plus a new method ChargeBattery(). Which is the best way to do this using inheritance?
hard
A. Make ElectricCar inherit Car and add ChargeBattery() method
B. Copy all Car code into ElectricCar and add ChargeBattery()
C. Make Car inherit ElectricCar and add ChargeBattery()
D. Create ElectricCar without inheriting Car and add ChargeBattery()

Solution

  1. Step 1: Understand inheritance for extending features

    Inheritance lets ElectricCar reuse Car's features and add new ones like ChargeBattery().
  2. Step 2: Evaluate options for best practice

    Make ElectricCar inherit Car and add ChargeBattery() method correctly uses inheritance to extend Car. Copying code duplicates work. Making Car inherit ElectricCar reverses the logic. Not inheriting loses reuse.
  3. Final Answer:

    Make ElectricCar inherit Car and add ChargeBattery() method -> Option A
  4. Quick Check:

    Extend with inheritance, add new methods [OK]
Hint: Extend existing class, add new methods in child [OK]
Common Mistakes:
  • Copy-pasting code instead of inheriting
  • Reversing inheritance direction
  • Not using inheritance to reuse code