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

How constructor chaining works in C Sharp (C#) - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to call the default constructor from the parameterized constructor.

C Sharp (C#)
public class Car {
    public Car() {
        Console.WriteLine("Default constructor called");
    }

    public Car(string model) : [1] {
        Console.WriteLine($"Car model: {model}");
    }
}
Drag options to blanks, or click blank then click option'
Athis()
Bbase()
CCar()
Dsuper()
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'base()' instead of 'this()' to call the same class constructor.
Trying to call constructor like a method without 'this()'.
2fill in blank
medium

Complete the code to chain constructors so the parameterless constructor calls the parameterized one.

C Sharp (C#)
public class Book {
    public Book(string title) {
        Console.WriteLine($"Title: {title}");
    }

    public Book() : [1] {
        Console.WriteLine("No title provided");
    }
}
Drag options to blanks, or click blank then click option'
Athis("Unknown")
Bbase()
CBook()
Dthis()
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'base()' which calls the base class constructor instead.
Calling 'this()' without arguments when the parameterized constructor needs one.
3fill in blank
hard

Fix the error in constructor chaining by completing the code.

C Sharp (C#)
public class Person {
    public Person(string name) {
        Console.WriteLine($"Name: {name}");
    }

    public Person() : [1] {
        Console.WriteLine("No name given");
    }
}
Drag options to blanks, or click blank then click option'
Athis()
BPerson()
Cbase()
Dthis(null)
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'this()' without arguments when the constructor needs one.
Using 'base()' which calls the base class constructor instead.
4fill in blank
hard

Fill both blanks to chain constructors correctly and print the age.

C Sharp (C#)
public class Animal {
    public Animal(string species, int age) {
        Console.WriteLine($"Species: {species}, Age: {age}");
    }

    public Animal(string species) : [1] {
        Console.WriteLine($"Species only: {species}");
    }

    public Animal() : [2] {
        Console.WriteLine("No details provided");
    }
}
Drag options to blanks, or click blank then click option'
Athis(species, 0)
Bthis()
Cthis("Unknown", 0)
Dbase()
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'base()' instead of 'this()' for chaining.
Not passing the required arguments in constructor chaining.
5fill in blank
hard

Fill all three blanks to chain constructors and initialize all fields.

C Sharp (C#)
public class Employee {
    public string Name;
    public int Id;
    public string Department;

    public Employee(string name, int id, string department) {
        Name = name;
        Id = id;
        Department = department;
        Console.WriteLine($"Name: {Name}, Id: {Id}, Department: {Department}");
    }

    public Employee(string name, int id) : [1] {
        Console.WriteLine("Department set to General");
    }

    public Employee(string name) : [2] {
        Console.WriteLine("Id set to 0 and Department to General");
    }

    public Employee() : [3] {
        Console.WriteLine("Default employee created");
    }
}
Drag options to blanks, or click blank then click option'
Athis(name, id, "General")
Bthis(name, 0, "General")
Cthis("Unknown", 0, "General")
Dbase()
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'base()' instead of 'this()' for chaining.
Not providing all required arguments in chained constructors.