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

Base class and derived class in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Inheritance Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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();
    }
}
A
Animal speaks
Animal speaks
Dog barks
B
Animal speaks
Dog barks
Animal speaks
C
Animal speaks
Dog barks
Dog barks
D
Dog barks
Dog barks
Dog barks
Attempts:
2 left
💡 Hint
Remember that virtual methods allow derived classes to override behavior.
Predict Output
intermediate
2: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();
    }
}
ADerived Print: x = 5
BDerived Print: x = 0
CBase Print
DCompilation error
Attempts:
2 left
💡 Hint
Think about the order of initialization when base constructor calls a virtual method.
🔧 Debug
advanced
2: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();
    }
}
ABecause the base class method is not marked virtual, so the derived method hides it but does not override.
BBecause the derived class method has a different name and signature.
CBecause the base class method is private and cannot be overridden.
DBecause the derived class method is static and cannot override instance methods.
Attempts:
2 left
💡 Hint
Check if the base method is virtual or not.
📝 Syntax
advanced
2:00remaining
Which option causes a compile-time error?
Which of the following code snippets will cause a compile-time error in C#?
Aclass Base { public virtual void Foo() {} } class Derived : Base { public override void Foo() {} }
Bclass Base { public virtual void Foo() {} } class Derived : Base { public void Foo() {} }
Cclass Base { public virtual void Foo() {} } class Derived : Base { public new void Foo() {} }
Dclass Base { public void Foo() {} } class Derived : Base { public override void Foo() {} }
Attempts:
2 left
💡 Hint
Override keyword requires the base method to be virtual or abstract.
🚀 Application
expert
2: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. ?
A4
B5
C6
D7
Attempts:
2 left
💡 Hint
Remember that private members of base class are not accessible in derived class instances.