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

Constructors and initialization in C Sharp (C#) - Interactive Code Practice

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

Complete the code to define a constructor for the class.

C Sharp (C#)
public class Person {
    public string Name;
    [1] Person(string name) {
        Name = name;
    }
}
Drag options to blanks, or click blank then click option'
Apublic
Bvoid
Cstatic
Dthis
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'void' as constructor return type
Using 'static' keyword for constructor
Omitting access modifier
2fill in blank
medium

Complete the constructor to initialize the Age field.

C Sharp (C#)
public class Person {
    public int Age;
    public Person(int age) {
        [1] = age;
    }
}
Drag options to blanks, or click blank then click option'
Athis.Age
Bage
CPerson.Age
DAge
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning parameter to itself
Using class name instead of this
Omitting this and causing ambiguity
3fill in blank
hard

Fix the error in the constructor to properly initialize the field.

C Sharp (C#)
public class Car {
    public string Model;
    public Car(string model) {
        Model = [1];
    }
}
Drag options to blanks, or click blank then click option'
Athis.Model
BModel
Cmodel
DCar.Model
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning field to itself
Using class name to access instance field
Using 'this' on the right side instead of parameter
4fill in blank
hard

Fill both blanks to create a constructor that initializes both fields.

C Sharp (C#)
public class Book {
    public string Title;
    public int Pages;
    public Book([1] title, [2] pages) {
        Title = title;
        Pages = pages;
    }
}
Drag options to blanks, or click blank then click option'
Apublic
Bprivate
Cint
Dstring
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong access modifier
Omitting parameter types
Using field types as parameter names
5fill in blank
hard

Fill all three blanks to create a constructor that initializes fields with different names.

C Sharp (C#)
public class Student {
    public string Name;
    public int Grade;
    [1] Student([2] studentName, int studentGrade) {
        Name = [3];
        Grade = studentGrade;
    }
}
Drag options to blanks, or click blank then click option'
Apublic
BstudentName
Dstring
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong parameter names
Omitting types
Assigning wrong variables