0
0
C Sharp (C#)programming~20 mins

Is-a relationship mental model in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
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.