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

How constructor chaining works in C Sharp (C#) - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Constructor Chaining Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of constructor chaining with base and this
What is the output of this C# program that uses constructor chaining with this and base?
C Sharp (C#)
using System;
class BaseClass {
    public BaseClass() {
        Console.WriteLine("BaseClass default constructor");
    }
    public BaseClass(string msg) {
        Console.WriteLine("BaseClass says: " + msg);
    }
}
class DerivedClass : BaseClass {
    public DerivedClass() : this("Hello") {
        Console.WriteLine("DerivedClass default constructor");
    }
    public DerivedClass(string msg) : base(msg) {
        Console.WriteLine("DerivedClass says: " + msg);
    }
}
class Program {
    static void Main() {
        var obj = new DerivedClass();
    }
}
A
BaseClass default constructor
DerivedClass says: Hello
DerivedClass default constructor
B
BaseClass says: Hello
DerivedClass says: Hello
DerivedClass default constructor
C
BaseClass says: Hello
DerivedClass default constructor
DerivedClass says: Hello
D
BaseClass default constructor
DerivedClass default constructor
DerivedClass says: Hello
Attempts:
2 left
💡 Hint
Remember that this calls another constructor in the same class before executing the body, and base calls the base class constructor.
Predict Output
intermediate
2:00remaining
Constructor chaining order in inheritance
What will be printed when this C# program runs?
C Sharp (C#)
using System;
class A {
    public A() {
        Console.WriteLine("A()");
    }
    public A(int x) {
        Console.WriteLine("A(int)");
    }
}
class B : A {
    public B() : base(5) {
        Console.WriteLine("B()");
    }
    public B(int x) : this() {
        Console.WriteLine("B(int)");
    }
}
class Program {
    static void Main() {
        var b = new B(10);
    }
}
A
A(int)
B()
B(int)
B
A()
B()
B(int)
C
A(int)
B(int)
B()
D
A()
B(int)
B()
Attempts:
2 left
💡 Hint
Check which constructors call which and in what order base constructors run.
🔧 Debug
advanced
2:00remaining
Identify the error in constructor chaining
What error will this C# code produce when compiled?
C Sharp (C#)
class Test {
    public Test() : this(5) {
    }
    public Test(int x) : this() {
    }
}
ANo error, compiles and runs fine
BCompile-time error: Missing base constructor call
CCompile-time error: Constructor chaining causes a cycle
DRuntime error: Stack overflow
Attempts:
2 left
💡 Hint
Check if constructors call each other in a loop.
Predict Output
advanced
2:00remaining
Output with multiple constructor chaining levels
What is the output of this C# program?
C Sharp (C#)
using System;
class X {
    public X() {
        Console.WriteLine("X()");
    }
    public X(string s) : this() {
        Console.WriteLine("X(string): " + s);
    }
}
class Y : X {
    public Y() : base("hello") {
        Console.WriteLine("Y()");
    }
    public Y(int n) : this() {
        Console.WriteLine("Y(int): " + n);
    }
}
class Program {
    static void Main() {
        var y = new Y(10);
    }
}
A
X()
X(string): hello
Y()
Y(int): 10
B
X(string): hello
X()
Y()
Y(int): 10
C
X()
Y()
X(string): hello
Y(int): 10
D
X()
Y(int): 10
X(string): hello
Y()
Attempts:
2 left
💡 Hint
Follow the constructor calls from Y(int) down to base.
🧠 Conceptual
expert
2:00remaining
Why constructor chaining is useful
Which of the following best explains why constructor chaining is useful in C#?
AIt prevents any constructor from calling base class constructors explicitly.
BIt allows multiple constructors to run in parallel threads for faster object creation.
CIt automatically generates default values for all fields without writing code.
DIt avoids code duplication by letting one constructor reuse another's initialization logic.
Attempts:
2 left
💡 Hint
Think about how constructors can share code.