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

Base keyword behavior in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Base Keyword Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of base keyword in method override
What is the output of this C# program when calling obj.Show()?
C Sharp (C#)
class Parent {
    public virtual void Show() {
        System.Console.WriteLine("Parent Show");
    }
}
class Child : Parent {
    public override void Show() {
        System.Console.WriteLine("Child Show Start");
        base.Show();
        System.Console.WriteLine("Child Show End");
    }
}

var obj = new Child();
obj.Show();
A
Child Show Start
Parent Show
Child Show End
B
Parent Show
Child Show Start
Child Show End
C
Child Show Start
Child Show End
Parent Show
D
Child Show Start
Child Show Start
Child Show End
Attempts:
2 left
💡 Hint
Remember that base.Show() calls the parent class method inside the override.
Predict Output
intermediate
2:00remaining
Using base constructor call
What will be printed when creating new Derived() with this code?
C Sharp (C#)
class Base {
    public Base() {
        System.Console.WriteLine("Base constructor");
    }
}
class Derived : Base {
    public Derived() : base() {
        System.Console.WriteLine("Derived constructor");
    }
}

var obj = new Derived();
ADerived constructor
B
Base constructor
Derived constructor
C
Derived constructor
Base constructor
DBase constructor
Attempts:
2 left
💡 Hint
The base constructor runs before the derived constructor body.
🔧 Debug
advanced
2:00remaining
Why does this code cause a compile error?
This code tries to call base.Method() inside a static method. What error will the compiler show?
C Sharp (C#)
class A {
    public void Method() { }
}
class B : A {
    public static void StaticMethod() {
        base.Method();
    }
}
AError: Keyword 'base' is not valid in a static context
BError: Cannot call instance method from static method
CError: Method does not exist in base class
DNo error, code compiles fine
Attempts:
2 left
💡 Hint
The base keyword requires an instance context.
🧠 Conceptual
advanced
2:00remaining
Effect of base keyword on property access
Given these classes, what will obj.Value print?
C Sharp (C#)
class Parent {
    public virtual int Value => 10;
}
class Child : Parent {
    public override int Value => base.Value + 5;
}

var obj = new Child();
System.Console.WriteLine(obj.Value);
ACompilation error
B10
C5
D15
Attempts:
2 left
💡 Hint
The base keyword accesses the parent property value.
Predict Output
expert
3:00remaining
Using base keyword with indexers and virtual methods
What is the output of this program?
C Sharp (C#)
class Base {
    protected virtual string this[int index] => $"Base[{index}]";
    public virtual string GetValue(int i) => this[i];
}
class Derived : Base {
    protected override string this[int index] => $"Derived[{index}]";
    public override string GetValue(int i) {
        return base.GetValue(i) + ", " + this[i];
    }
}

var obj = new Derived();
System.Console.WriteLine(obj.GetValue(3));
ABase[3], Derived[3]
BDerived[3], Base[3]
CDerived[3], Derived[3]
DBase[3], Base[3]
Attempts:
2 left
💡 Hint
The base.GetValue(i) calls the base class method which uses the base indexer.