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

Is-a relationship mental model 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
🎖️
Is-a Relationship Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of method call in inheritance
What is the output of the following C# code that demonstrates the is-a relationship using inheritance?
C Sharp (C#)
class Animal {
    public virtual string Speak() {
        return "Animal sound";
    }
}

class Dog : Animal {
    public override string Speak() {
        return "Bark";
    }
}

class Program {
    static void Main() {
        Animal myPet = new Dog();
        System.Console.WriteLine(myPet.Speak());
    }
}
ABark
BCompile-time error
CRuntime exception
DAnimal sound
Attempts:
2 left
💡 Hint
Think about which method is called when a derived class overrides a base class method.
🧠 Conceptual
intermediate
1:30remaining
Understanding the is-a relationship
Which statement best describes the is-a relationship in object-oriented programming?
AA class creates instances of other classes.
BA class contains another class as a member variable.
CA class implements multiple unrelated interfaces.
DA class inherits from another class, meaning it is a specialized type of that class.
Attempts:
2 left
💡 Hint
Think about inheritance and how one class relates to another as a type.
Predict Output
advanced
2:00remaining
Output with casting and is-a relationship
What will be the output of this C# program that uses casting and the is-a relationship?
C Sharp (C#)
class Vehicle {
    public virtual string Describe() => "Vehicle";
}

class Car : Vehicle {
    public override string Describe() => "Car";
}

class Program {
    static void Main() {
        Vehicle v = new Car();
        if (v is Car) {
            Car c = (Car)v;
            System.Console.WriteLine(c.Describe());
        } else {
            System.Console.WriteLine(v.Describe());
        }
    }
}
AVehicle
BInvalid cast exception
CCar
DCompile-time error
Attempts:
2 left
💡 Hint
Check the type of the object before casting and which Describe method is called.
Predict Output
advanced
2:00remaining
Output of method hiding vs overriding
What is the output of this C# code demonstrating method hiding and the is-a relationship?
C Sharp (C#)
class Base {
    public string Show() => "Base Show";
}

class Derived : Base {
    public new string Show() => "Derived Show";
}

class Program {
    static void Main() {
        Base obj = new Derived();
        System.Console.WriteLine(obj.Show());
    }
}
ARuntime exception
BBase Show
CCompile-time error
DDerived Show
Attempts:
2 left
💡 Hint
Consider that method hiding does not override the base method.
🧠 Conceptual
expert
1:30remaining
Is-a relationship and interface implementation
Which statement correctly explains the is-a relationship when a class implements an interface in C#?
AImplementing an interface means the class is a subtype of the interface, establishing an is-a relationship.
BImplementing an interface creates a has-a relationship, not is-a.
CInterfaces cannot express is-a relationships in C#.
DA class implementing an interface must inherit from a base class to have an is-a relationship.
Attempts:
2 left
💡 Hint
Think about how interfaces define a contract that the class agrees to fulfill.

Practice

(1/5)
1. What does the Is-a relationship represent in C# programming?
easy
A. A class contains another class as a member
B. A class inherits properties and methods from another class
C. A class is converted into another class
D. A class is unrelated to any other class

Solution

  1. Step 1: Understand inheritance concept

    The Is-a relationship means one class inherits from another, gaining its features.
  2. Step 2: Identify correct description

    A class inherits properties and methods from another class correctly describes inheritance, while others describe different concepts.
  3. Final Answer:

    A class inherits properties and methods from another class -> Option B
  4. Quick Check:

    Is-a means inheritance = B [OK]
Hint: Is-a means inheritance, not containment or conversion [OK]
Common Mistakes:
  • Confusing Is-a with Has-a (containment)
  • Thinking Is-a means type conversion
  • Assuming unrelated classes have Is-a relationship
2. Which of the following is the correct syntax to express an Is-a relationship in C#?
easy
A. class Dog inherits Animal {}
B. class Dog extends Animal {}
C. class Dog : Animal {}
D. class Dog -> Animal {}

Solution

  1. Step 1: Recall C# inheritance syntax

    In C#, the colon (:) symbol is used to indicate inheritance.
  2. Step 2: Compare options

    class Dog : Animal {} uses the correct syntax 'class Dog : Animal {}'. Others use incorrect keywords or symbols.
  3. Final Answer:

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

    C# inheritance uses ':' = C [OK]
Hint: Use ':' to inherit in C# classes [OK]
Common Mistakes:
  • Using 'inherits' keyword (not valid in C#)
  • Using 'extends' (Java syntax)
  • Using arrows or other symbols
3. Consider this code:
class Animal { public string Speak() => "Sound"; } class Dog : Animal { }

What is the output of:
var d = new Dog(); Console.WriteLine(d.Speak());
medium
A. Runtime error
B. Dog
C. Compile error
D. Sound

Solution

  1. Step 1: Understand inheritance effect

    Dog inherits from Animal, so Dog has the Speak() method.
  2. Step 2: Predict method call output

    Calling d.Speak() returns "Sound" from Animal class.
  3. Final Answer:

    Sound -> Option D
  4. Quick Check:

    Inherited method returns "Sound" = A [OK]
Hint: Inherited methods can be called on child objects [OK]
Common Mistakes:
  • Expecting Dog to override Speak() automatically
  • Thinking code causes compile or runtime error
  • Confusing output with class name
4. Identify the error in this code snippet:
class Animal { } class Dog Animal { }
medium
A. Missing colon ':' between Dog and Animal
B. Dog cannot inherit from Animal
C. Animal class must be abstract
D. Dog class must have a constructor

Solution

  1. Step 1: Check inheritance syntax

    In C#, inheritance requires a colon ':' between child and parent class names.
  2. Step 2: Identify missing symbol

    The code misses ':' between Dog and Animal, causing syntax error.
  3. Final Answer:

    Missing colon ':' between Dog and Animal -> Option A
  4. Quick Check:

    Inheritance needs ':' = A [OK]
Hint: Always use ':' to inherit in C# [OK]
Common Mistakes:
  • Forgetting the colon ':'
  • Thinking parent class must be abstract
  • Assuming constructor is mandatory
5. Given these classes:
class Vehicle { public virtual string Move() => "Moving"; } class Car : Vehicle { public override string Move() => "Car is moving"; } class Bike : Vehicle { }

What will be the output of:
Vehicle v1 = new Car(); Vehicle v2 = new Bike(); Console.WriteLine(v1.Move()); Console.WriteLine(v2.Move());
hard
A. Car is moving\nMoving
B. Moving\nMoving
C. Car is moving\nBike is moving
D. Compile error due to missing override

Solution

  1. Step 1: Understand virtual and override behavior

    Car overrides Move(), so v1.Move() calls Car's version. Bike does not override, so v2.Move() calls Vehicle's version.
  2. Step 2: Predict output lines

    v1.Move() outputs "Car is moving"; v2.Move() outputs "Moving".
  3. Final Answer:

    Car is moving\nMoving -> Option A
  4. Quick Check:

    Override changes output, no override uses base = D [OK]
Hint: Override changes method output; no override uses base method [OK]
Common Mistakes:
  • Expecting Bike to output 'Bike is moving'
  • Thinking missing override causes compile error
  • Confusing which method is called