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

Constructor overloading in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
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.