Bird
Raised Fist0
C Sharp (C#)programming~20 mins

Constructors and initialization in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Constructor Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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();
    }
}
A
Constructor with name: Unknown
Default constructor called
B
Default constructor called
Constructor with name: Unknown
CDefault constructor called
D
Constructor with name: 
Default constructor called
Attempts:
2 left
💡 Hint
Remember that constructor chaining calls the chained constructor first before executing the body of the calling constructor.
Predict Output
intermediate
2: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();
    }
}
Ax = 0, y = 10
Bx = 5, y = 10
Cx = 0, y = 0
Dx = 5, y = 0
Attempts:
2 left
💡 Hint
Fields initialized inline are set before the constructor body runs.
Predict Output
advanced
2: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();
    }
}
A
Instance constructor called
Instance constructor called
Static constructor called
B
Instance constructor called
Static constructor called
Instance constructor called
C
Static constructor called
Instance constructor called
D
Static constructor called
Instance constructor called
Instance constructor called
Attempts:
2 left
💡 Hint
Static constructors run once before any instance is created or static member accessed.
Predict Output
advanced
2: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");
    }
}
A
Number is 10
Done
B
Number is 20
Done
CCompilation error due to assignment in ChangeNumber method
D
Number is 0
Done
Attempts:
2 left
💡 Hint
Readonly fields can only be assigned in declaration or constructor.
Predict Output
expert
3: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();
    }
}
A
Base constructor
Derived Print: x = 5
Derived constructor
B
Derived constructor
Base constructor
Derived Print: x = 0
C
Base constructor
Derived Print: x = 0
Derived constructor
D
Derived constructor
Base constructor
Derived Print: x = 5
Attempts:
2 left
💡 Hint
During base constructor, derived fields are not yet initialized.

Practice

(1/5)
1. What is the main purpose of a constructor in a C# class?
easy
A. To define methods that return values
B. To declare variables inside a class
C. To initialize new objects with starting values
D. To inherit properties from another class

Solution

  1. Step 1: Understand what constructors do

    Constructors are special methods that run when an object is created to set initial values.
  2. Step 2: Compare options with constructor purpose

    Only To initialize new objects with starting values describes initializing new objects, which matches the constructor's role.
  3. Final Answer:

    To initialize new objects with starting values -> Option C
  4. Quick Check:

    Constructor purpose = initialize objects [OK]
Hint: Constructors set initial values when creating objects [OK]
Common Mistakes:
  • Confusing constructors with regular methods
  • Thinking constructors return values
  • Mixing constructors with inheritance
2. Which of the following is the correct syntax for a constructor in C# for a class named Car?
easy
A. public Car() { }
B. void Car() { }
C. public void Car() { }
D. public Car(void) { }

Solution

  1. Step 1: Recall constructor syntax rules

    Constructors have the same name as the class and no return type, but must have an access modifier like public.
  2. Step 2: Check each option

    public Car() { } matches: public + class name + parentheses + no return type. Others have void return or wrong syntax.
  3. Final Answer:

    public Car() { } -> Option A
  4. Quick Check:

    Constructor syntax = public ClassName() [OK]
Hint: Constructor name = class name, no return type [OK]
Common Mistakes:
  • Adding a return type like void
  • Using incorrect parameter syntax
  • Omitting access modifier
3. What will be the output of the following C# code?
class Person {
  public string Name;
  public Person(string name) {
    Name = name;
  }
}

var p = new Person("Anna");
Console.WriteLine(p.Name);
medium
A. Compilation error
B. Anna
C. null
D. Name

Solution

  1. Step 1: Understand constructor usage

    The constructor sets the Name field to the passed string "Anna" when creating the Person object.
  2. Step 2: Check output of Console.WriteLine

    Since p.Name was set to "Anna", printing p.Name outputs "Anna".
  3. Final Answer:

    Anna -> Option B
  4. Quick Check:

    Constructor sets Name = "Anna" so output = Anna [OK]
Hint: Constructor sets fields; output shows assigned value [OK]
Common Mistakes:
  • Assuming default null value instead of assigned
  • Confusing field name with value
  • Expecting compilation error due to constructor
4. Identify the error in this C# class constructor and how to fix it:
class Book {
  public string Title;
  public Book(string title) {
    title = Title;
  }
}
medium
A. Title should be private, not public
B. Constructor name should be lowercase book
C. Missing return type void in constructor
D. The assignment is reversed; should be Title = title;

Solution

  1. Step 1: Analyze the assignment inside constructor

    The code assigns title = Title, which sets the parameter to the field's value, not the other way around.
  2. Step 2: Correct the assignment direction

    It should assign the field Title to the parameter value: Title = title; to initialize properly.
  3. Final Answer:

    The assignment is reversed; should be Title = title; -> Option D
  4. Quick Check:

    Field = parameter to initialize correctly [OK]
Hint: Assign field = parameter inside constructor [OK]
Common Mistakes:
  • Reversing assignment direction
  • Changing constructor name incorrectly
  • Adding return type to constructor
5. Given this class with two constructors:
class Rectangle {
  public int Width, Height;
  public Rectangle() {
    Width = 1;
    Height = 1;
  }
  public Rectangle(int size) {
    Width = size;
    Height = size;
  }
}

var r1 = new Rectangle();
var r2 = new Rectangle(5);
Console.WriteLine(r1.Width + "," + r1.Height);
Console.WriteLine(r2.Width + "," + r2.Height);

What is the output?
hard
A. 1,1 5,5
B. 0,0 5,5
C. 1,1 1,1
D. Compilation error due to constructor overload

Solution

  1. Step 1: Understand constructor overloading

    The class has two constructors: one with no parameters sets Width and Height to 1; the other sets both to the given size.
  2. Step 2: Trace object creation and output

    r1 uses the no-parameter constructor, so Width=1, Height=1. r2 uses the int parameter constructor with 5, so Width=5, Height=5.
  3. Final Answer:

    1,1 5,5 -> Option A
  4. Quick Check:

    Overloaded constructors set different sizes correctly [OK]
Hint: Overloaded constructors run based on arguments [OK]
Common Mistakes:
  • Assuming default values are zero
  • Thinking constructor overload causes error
  • Mixing up which constructor runs