Combining base-class behavior with derived behavior: which statement should be added so that the output is exactly Welcome to CuriousTab.com! for the following code? class A { public void fun() { Console.Write("Welcome"); } } class B : A { public void fun() { // [*** Add statement here ***] Console.WriteLine(" to CuriousTab.com!"); } }

Difficulty: Easy

Correct Answer: base.fun();

Explanation:

Introduction / Context:Calling base class members from a derived class is common when you want to append or prepend additional behavior. Here, we wish to print a prefix from the base method and then a suffix in the derived method.

Given Data / Assumptions:

  • A.fun() prints “Welcome”.
  • B.fun() needs to print “Welcome to CuriousTab.com!”.
  • There is no virtual/override here; we simply want to reuse base logic inside B.fun().

Concept / Approach:Inside a derived class, the base qualifier calls the base implementation. Using base.fun() writes “Welcome” to the console, after which the derived method writes the trailing text.

Step-by-Step Solution:

Insert base.fun(); as the first statement of B.fun().Then the existing WriteLine adds “ to CuriousTab.com!”.Combined output: “Welcome to CuriousTab.com!”.

Verification / Alternative check:Running with base.fun(); prints the required phrase on a single line.

Why Other Options Are Wrong:A::fun() and mybase.fun() are not valid C# syntax. Calling fun() without qualification inside B would recursively call B.fun() leading to infinite recursion. A.fun() is not the way to invoke an instance base member from an instance method.

Common Pitfalls:Accidentally recursing by calling fun() unqualified; using non-C# syntax from other languages.

Final Answer:base.fun();

More Questions from Inheritance

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion