Challenge - 5 Problems
Inheritance Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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(); } }
Attempts:
2 left
💡 Hint
Remember that virtual methods allow derived classes to override behavior.
✗ Incorrect
The base class method Speak is virtual and overridden in Dog. When calling Speak on a base class reference to a Dog object, the Dog's override is called.
❓ Predict Output
intermediate2: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(); } }
Attempts:
2 left
💡 Hint
Think about the order of initialization when base constructor calls a virtual method.
✗ Incorrect
During base class constructor execution, derived class fields are not yet initialized, so x is 0 (default int).
🔧 Debug
advanced2: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(); } }
Attempts:
2 left
💡 Hint
Check if the base method is virtual or not.
✗ Incorrect
Only virtual methods can be overridden. Here, Show in Base is not virtual, so Derived.Show hides it but does not override. The call uses Base's method.
📝 Syntax
advanced2:00remaining
Which option causes a compile-time error?
Which of the following code snippets will cause a compile-time error in C#?
Attempts:
2 left
💡 Hint
Override keyword requires the base method to be virtual or abstract.
✗ Incorrect
Option D tries to override a non-virtual method, causing a compile-time error.
🚀 Application
expert2: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. ?
Attempts:
2 left
💡 Hint
Remember that private members of base class are not accessible in derived class instances.
✗ Incorrect
Accessible members are: PublicMethod, PublicProperty, DerivedMethod, DerivedProperty, and Protected members are NOT accessible via instance outside class, so total 4.