Challenge - 5 Problems
Constructor Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of constructor chaining in C#
What is the output of this C# program when creating an instance of
Person with new Person()?C Sharp (C#)
using System; class Person { public string Name; public int Age; public Person() : this("Unknown") { Console.WriteLine("Default constructor called"); } public Person(string name) { Name = name; Age = 0; Console.WriteLine($"Constructor with name: {Name}"); } } class Program { static void Main() { Person p = new Person(); } }
Attempts:
2 left
💡 Hint
Remember that constructor chaining calls the chained constructor first before executing the body of the calling constructor.
✗ Incorrect
The parameterless constructor calls the constructor with a string parameter using ': this("Unknown")'. So first, the constructor with the name parameter runs and prints 'Constructor with name: Unknown'. Then control returns to the parameterless constructor, which prints 'Default constructor called'.
❓ Predict Output
intermediate2:00remaining
Field initialization order in C# class
What is the output of this C# program?
C Sharp (C#)
using System; class Sample { private int x = 5; private int y; public Sample() { y = x * 2; Console.WriteLine($"x = {x}, y = {y}"); } } class Program { static void Main() { Sample s = new Sample(); } }
Attempts:
2 left
💡 Hint
Fields initialized inline are set before the constructor body runs.
✗ Incorrect
The field 'x' is initialized to 5 before the constructor runs. Then in the constructor, 'y' is set to 'x * 2', which is 10. So the output is 'x = 5, y = 10'.
❓ Predict Output
advanced2:00remaining
Output of static and instance constructors
What is the output when this C# program runs?
C Sharp (C#)
using System; class Demo { static Demo() { Console.WriteLine("Static constructor called"); } public Demo() { Console.WriteLine("Instance constructor called"); } } class Program { static void Main() { Demo d1 = new Demo(); Demo d2 = new Demo(); } }
Attempts:
2 left
💡 Hint
Static constructors run once before any instance is created or static member accessed.
✗ Incorrect
The static constructor runs once before the first instance is created, printing 'Static constructor called'. Then each instance constructor runs printing 'Instance constructor called'. So the output is the static constructor message once, then two instance constructor messages.
❓ Predict Output
advanced2:00remaining
Output of readonly field initialization
What is the output of this C# program?
C Sharp (C#)
using System; class Test { private readonly int number; public Test(int num) { number = num; Console.WriteLine($"Number is {number}"); } public void ChangeNumber(int newNum) { // number = newNum; // Uncommenting this line causes error } } class Program { static void Main() { Test t = new Test(10); t.ChangeNumber(20); Console.WriteLine("Done"); } }
Attempts:
2 left
💡 Hint
Readonly fields can only be assigned in declaration or constructor.
✗ Incorrect
The readonly field 'number' is assigned in the constructor to 10. The ChangeNumber method does not change it because the assignment is commented out. So the output is 'Number is 10' and then 'Done'.
❓ Predict Output
expert3:00remaining
Output of complex constructor and field initialization order
What is the output of this C# program?
C Sharp (C#)
using System; class Base { public Base() { Console.WriteLine("Base constructor"); Print(); } public virtual void Print() { Console.WriteLine("Base Print"); } } class Derived : Base { private int x = 5; public Derived() { Console.WriteLine("Derived constructor"); } public override void Print() { Console.WriteLine($"Derived Print: x = {x}"); } } class Program { static void Main() { Derived d = new Derived(); } }
Attempts:
2 left
💡 Hint
During base constructor, derived fields are not yet initialized.
✗ Incorrect
When the base constructor calls the virtual method Print, the derived override runs but the derived field 'x' is still at default 0 because field initializers run after base constructor. Then the derived constructor runs printing 'Derived constructor'.