Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'void' as constructor return type
Using 'static' keyword for constructor
Omitting access modifier
✗ Incorrect
The constructor must have the public access modifier to be accessible and properly defined.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning parameter to itself
Using class name instead of this
Omitting this and causing ambiguity
✗ Incorrect
Use this.Age to refer to the instance field and assign the parameter value.
3fill in blank
hardFix 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'
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
✗ Incorrect
The parameter model should be assigned to the field Model.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong access modifier
Omitting parameter types
Using field types as parameter names
✗ Incorrect
Constructor parameters need types and the constructor should be public.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong parameter names
Omitting types
Assigning wrong variables
✗ Incorrect
The constructor is public, parameters need types, and the field Name is assigned the parameter studentName.