Challenge - 5 Problems
Constructor Overloading Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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(); } }
Attempts:
2 left
💡 Hint
Look at which constructor is called for each object.
✗ Incorrect
The first Box uses the parameterless constructor setting all dimensions to 1. The second Box uses the constructor with parameters, setting length=2, width=3, height=4.
❓ Predict Output
intermediate2: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(); } }
Attempts:
2 left
💡 Hint
Check which constructor matches the argument list.
✗ Incorrect
The constructor with one string parameter sets name to the argument and age to 18.
❓ Predict Output
advanced2: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(); } }
Attempts:
2 left
💡 Hint
Follow the constructor calls from the parameterless to the full constructor.
✗ Incorrect
Each constructor calls the next with default values. The year defaults to 0 except for the two-parameter constructor which sets year to 2020.
❓ Predict Output
advanced2: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}"); } }
Attempts:
2 left
💡 Hint
Check the constructors defined and the arguments passed.
✗ Incorrect
No constructor matches two parameters (int, string), so compiler error occurs.
🧠 Conceptual
expert2: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 } }
Attempts:
2 left
💡 Hint
Count each constructor call in the chain starting from the parameterless one.
✗ Incorrect
The parameterless constructor calls the one with int, which calls the one with int and string, so 3 constructors are called in total.