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

Constructor overloading 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 Overloading Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of overloaded constructors with default values
What is the output of this C# program using constructor overloading?
C Sharp (C#)
using System;
class Box {
    public int length, width, height;
    public Box() {
        length = 1; width = 1; height = 1;
    }
    public Box(int l, int w, int h) {
        length = l; width = w; height = h;
    }
    public void Display() {
        Console.WriteLine($"{length} {width} {height}");
    }
}
class Program {
    static void Main() {
        Box b1 = new Box();
        Box b2 = new Box(2, 3, 4);
        b1.Display();
        b2.Display();
    }
}
A1 1 1\n2 3 4
B0 0 0\n2 3 4
C1 1 1\n0 0 0
D2 3 4\n1 1 1
Attempts:
2 left
💡 Hint
Look at which constructor is called for each object.
Predict Output
intermediate
2:00remaining
Which constructor is called?
Given this class with overloaded constructors, what will be printed when creating new Person("Alice")?
C Sharp (C#)
using System;
class Person {
    public string name;
    public int age;
    public Person() {
        name = "Unknown";
        age = 0;
    }
    public Person(string n) {
        name = n;
        age = 18;
    }
    public Person(string n, int a) {
        name = n;
        age = a;
    }
    public void Display() {
        Console.WriteLine($"Name: {name}, Age: {age}");
    }
}
class Program {
    static void Main() {
        Person p = new Person("Alice");
        p.Display();
    }
}
AName: Unknown, Age: 0
BName: Alice, Age: 0
CName: Unknown, Age: 18
DName: Alice, Age: 18
Attempts:
2 left
💡 Hint
Check which constructor matches the argument list.
Predict Output
advanced
2:00remaining
Output with constructor chaining
What is the output of this program that uses constructor chaining in C#?
C Sharp (C#)
using System;
class Car {
    public string brand;
    public string model;
    public int year;

    public Car() : this("Unknown", "Unknown", 0) {}
    public Car(string brand) : this(brand, "Unknown", 0) {}
    public Car(string brand, string model) : this(brand, model, 2020) {}
    public Car(string brand, string model, int year) {
        this.brand = brand;
        this.model = model;
        this.year = year;
    }
    public void Display() {
        Console.WriteLine($"{brand} {model} {year}");
    }
}
class Program {
    static void Main() {
        Car c1 = new Car();
        Car c2 = new Car("Toyota");
        Car c3 = new Car("Honda", "Civic");
        Car c4 = new Car("Ford", "Mustang", 1969);
        c1.Display();
        c2.Display();
        c3.Display();
        c4.Display();
    }
}
AUnknown Unknown 0\nToyota Unknown 2020\nHonda Civic 2020\nFord Mustang 1969
BUnknown Unknown 0\nToyota Unknown 0\nHonda Civic 0\nFord Mustang 1969
CUnknown Unknown 0\nToyota Unknown 0\nHonda Civic 2020\nFord Mustang 1969
DUnknown Unknown 2020\nToyota Unknown 0\nHonda Civic 2020\nFord Mustang 1969
Attempts:
2 left
💡 Hint
Follow the constructor calls from the parameterless to the full constructor.
Predict Output
advanced
2:00remaining
What error occurs with this constructor call?
What error will this code produce when trying to create new Employee(123, "John")?
C Sharp (C#)
using System;
class Employee {
    public int id;
    public string name;
    public Employee(string name) {
        this.name = name;
        id = 0;
    }
    public Employee(int id) {
        this.id = id;
        name = "Unknown";
    }
}
class Program {
    static void Main() {
        Employee e = new Employee(123, "John");
        Console.WriteLine($"ID: {e.id}, Name: {e.name}");
    }
}
ACompile-time error: no constructor takes 2 arguments
BRuntime error: NullReferenceException
COutput: ID: 123, Name: John
DOutput: ID: 0, Name: Unknown
Attempts:
2 left
💡 Hint
Check the constructors defined and the arguments passed.
🧠 Conceptual
expert
2:00remaining
How many constructors are called in this chain?
Consider this class with constructor overloading and chaining. How many constructors are called when creating new Gadget()?
C Sharp (C#)
class Gadget {
    public Gadget() : this(10) {}
    public Gadget(int size) : this(size, "Standard") {}
    public Gadget(int size, string type) {
        // Initialization code
    }
}
A1
B3
C2
D0
Attempts:
2 left
💡 Hint
Count each constructor call in the chain starting from the parameterless one.

Practice

(1/5)
1. What does constructor overloading in C# allow you to do?
easy
A. Override methods with the same name
B. Create multiple constructors with different parameter lists in the same class
C. Use constructors without parameters only
D. Create only one constructor per class

Solution

  1. Step 1: Understand constructor overloading concept

    Constructor overloading means having more than one constructor in a class, each with a different set of parameters.
  2. Step 2: Identify what overloading allows

    This allows creating objects in different ways depending on the parameters passed.
  3. Final Answer:

    Create multiple constructors with different parameter lists in the same class -> Option B
  4. Quick Check:

    Constructor overloading = multiple constructors with different parameters [OK]
Hint: Multiple constructors differ by parameter list only [OK]
Common Mistakes:
  • Thinking only one constructor is allowed
  • Confusing overloading with overriding
  • Believing constructors must have no parameters
2. Which of the following is a correct constructor overloading syntax in C#?
easy
A. public class Car { public Car() {} public Car(string model) {} }
B. public class Car { public void Car() {} public void Car(string model) {} }
C. public class Car { public Car() {} public Car() {} }
D. public class Car { Car() {} Car() {} }

Solution

  1. Step 1: Check constructor syntax

    Constructors must have the same name as the class and no return type.
  2. Step 2: Identify correct overloading

    public class Car { public Car() {} public Car(string model) {} } has two constructors with different parameters and correct syntax.
  3. Final Answer:

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

    Constructor syntax correct and overloaded by parameters [OK]
Hint: Constructors have no return type and match class name [OK]
Common Mistakes:
  • Adding return type to constructors
  • Defining multiple constructors with same parameters
  • Omitting access modifier (not mandatory but common style)
3. What will be the output of this code?
class Box {
  public int length;
  public Box() { length = 5; }
  public Box(int l) { length = l; }
}
class Program {
  static void Main() {
    Box b1 = new Box();
    Box b2 = new Box(10);
    Console.WriteLine(b1.length + ", " + b2.length);
  }
}
medium
A. 5, 10
B. 0, 10
C. 5, 5
D. 10, 10

Solution

  1. Step 1: Analyze constructors called

    b1 uses the parameterless constructor setting length = 5; b2 uses the constructor with int parameter setting length = 10.
  2. Step 2: Determine printed values

    Console.WriteLine prints b1.length (5) and b2.length (10) separated by a comma.
  3. Final Answer:

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

    Default constructor sets 5, parameterized sets 10 [OK]
Hint: Check which constructor is called for each object [OK]
Common Mistakes:
  • Assuming default int value 0 instead of assigned 5
  • Confusing which constructor runs for each object
  • Mixing up output order
4. Identify the error in this constructor overloading code:
class Person {
  public string name;
  public Person(string n) { name = n; }
  public Person(string n) { name = n.ToUpper(); }
}
medium
A. Constructor name does not match class name
B. Missing return type in constructors
C. Duplicate constructor with same parameter list
D. Cannot assign string to name

Solution

  1. Step 1: Check constructor parameter lists

    Both constructors have the same parameter type and count (string n), causing duplication.
  2. Step 2: Understand overloading rules

    Constructors must differ by parameter types or count to overload; identical signatures cause error.
  3. Final Answer:

    Duplicate constructor with same parameter list -> Option C
  4. Quick Check:

    Same parameters = duplicate constructor error [OK]
Hint: Constructor signatures must differ by parameters [OK]
Common Mistakes:
  • Thinking constructors can differ by body only
  • Adding return type mistakenly
  • Ignoring parameter list uniqueness
5. You want to create a class Rectangle with overloaded constructors:
- One constructor takes no parameters and sets width and height to 1.
- Another takes one parameter and sets both width and height to that value.
- Another takes two parameters to set width and height separately.
Which of these constructor definitions correctly implements this?
hard
A. public Rectangle() { width = 1; height = 1; } public Rectangle(int w, int h) { width = w; height = h; } public Rectangle(int w, int h) { width = w; height = h; }
B. public Rectangle() { width = 1; height = 1; } public Rectangle(int size) { width = size; height = size; } public Rectangle(int size) { width = size; height = size; }
C. public Rectangle(int w, int h) { width = w; height = h; } public Rectangle() { width = 1; height = 1; } public Rectangle(int size) { width = size; }
D. public Rectangle() { width = 1; height = 1; } public Rectangle(int size) { width = size; height = size; } public Rectangle(int w, int h) { width = w; height = h; }

Solution

  1. Step 1: Check parameter lists for uniqueness

    public Rectangle() { width = 1; height = 1; } public Rectangle(int size) { width = size; height = size; } public Rectangle(int w, int h) { width = w; height = h; } has three constructors with distinct parameter lists: no parameters, one int, and two ints.
  2. Step 2: Verify each constructor sets values correctly

    Each constructor sets width and height as required: default 1, same size, or separate sizes.
  3. Final Answer:

    public Rectangle() { width = 1; height = 1; } public Rectangle(int size) { width = size; height = size; } public Rectangle(int w, int h) { width = w; height = h; } -> Option D
  4. Quick Check:

    Distinct parameter lists and correct assignments [OK]
Hint: Each constructor must have unique parameter count or types [OK]
Common Mistakes:
  • Defining two constructors with same parameter types
  • Mixing order of constructors causing confusion
  • Not setting default values in parameterless constructor