Challenge - 5 Problems
Is-a Relationship Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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()); } }
Attempts:
2 left
💡 Hint
Think about which method is called when a derived class overrides a base class method.
✗ Incorrect
The variable myPet is of type Animal but refers to an instance of Dog. Because Speak() is overridden in Dog, the Dog's Speak() method is called, printing "Bark".
🧠 Conceptual
intermediate1:30remaining
Understanding the is-a relationship
Which statement best describes the is-a relationship in object-oriented programming?
Attempts:
2 left
💡 Hint
Think about inheritance and how one class relates to another as a type.
✗ Incorrect
The is-a relationship means one class is a subtype of another through inheritance, showing a specialized version of the base class.
❓ Predict Output
advanced2: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()); } } }
Attempts:
2 left
💡 Hint
Check the type of the object before casting and which Describe method is called.
✗ Incorrect
The variable v refers to a Car object, so the 'is' check passes. Casting to Car is safe, and the Car's Describe method returns "Car".
❓ Predict Output
advanced2: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()); } }
Attempts:
2 left
💡 Hint
Consider that method hiding does not override the base method.
✗ Incorrect
The variable obj is of type Base, so the Base's Show method is called, even though the object is Derived. The new keyword hides the method but does not override it.
🧠 Conceptual
expert1:30remaining
Is-a relationship and interface implementation
Which statement correctly explains the is-a relationship when a class implements an interface in C#?
Attempts:
2 left
💡 Hint
Think about how interfaces define a contract that the class agrees to fulfill.
✗ Incorrect
When a class implements an interface, it is considered a subtype of that interface, so it has an is-a relationship with the interface type.