Challenge - 5 Problems
Base Keyword Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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();
Attempts:
2 left
💡 Hint
Remember that
base.Show() calls the parent class method inside the override.✗ Incorrect
The
Child class overrides Show(). Inside it, it prints "Child Show Start", then calls base.Show() which runs the Parent version printing "Parent Show", then prints "Child Show End".❓ Predict Output
intermediate2: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();
Attempts:
2 left
💡 Hint
The base constructor runs before the derived constructor body.
✗ Incorrect
The
Derived constructor explicitly calls base(), so the Base constructor runs first printing "Base constructor", then the Derived constructor prints "Derived constructor".🔧 Debug
advanced2: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(); } }
Attempts:
2 left
💡 Hint
The
base keyword requires an instance context.✗ Incorrect
The
base keyword can only be used inside instance methods because it refers to the current instance's base class. Static methods have no instance, so using base causes a compile error.🧠 Conceptual
advanced2: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);
Attempts:
2 left
💡 Hint
The
base keyword accesses the parent property value.✗ Incorrect
The
Child overrides Value and returns base.Value + 5. Since base.Value is 10, the result is 15.❓ Predict Output
expert3: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));
Attempts:
2 left
💡 Hint
The
base.GetValue(i) calls the base class method which uses the base indexer.✗ Incorrect
The
base.GetValue(i) calls Base.GetValue which returns this[i]. Because this[i] is virtual and overridden, it calls Derived's indexer. So both calls use Derived's indexer. So the output is "Derived[3], Derived[3]".