Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to pass the object to the method.
C Sharp (C#)
class Program { static void Main() { Person p = new Person(); UpdateName([1]); } static void UpdateName(Person person) { person.Name = "Alice"; } } class Person { public string Name { get; set; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the class name instead of the object variable.
Passing null instead of the object.
✗ Incorrect
You need to pass the existing object p to the method to update its name.
2fill in blank
mediumComplete the method call to change the age of the person.
C Sharp (C#)
class Program { static void Main() { Person p = new Person(); SetAge([1], 30); } static void SetAge(Person person, int age) { person.Age = age; } } class Person { public int Age { get; set; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a new object instead of the existing one.
Passing the class name instead of an object.
✗ Incorrect
You must pass the existing object p to update its age.
3fill in blank
hardFix the error in the method to correctly update the person's city.
C Sharp (C#)
class Program { static void Main() { Person p = new Person(); ChangeCity(p); System.Console.WriteLine(p.City); } static void ChangeCity(Person person) { person.City = [1]; } } class Person { public string City { get; set; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around the string.
Using an undefined variable.
✗ Incorrect
The city name must be a string literal with quotes.
4fill in blank
hardFill both blanks to create a method that updates the person's name and age.
C Sharp (C#)
class Program { static void Main() { Person p = new Person(); UpdatePerson(p, [1]); System.Console.WriteLine($"Name: {p.Name}, Age: {p.Age}"); } static void UpdatePerson(Person person, int newAge) { person.Name = [2]; person.Age = newAge; } } class Person { public string Name { get; set; } public int Age { get; set; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the wrong type for age.
Not using quotes for the name string.
✗ Incorrect
The method call passes the new age 25 and sets the name to "Bob".
5fill in blank
hardFill all three blanks to create a method that updates name, age, and city of a person.
C Sharp (C#)
class Program { static void Main() { Person p = new Person(); UpdateDetails(p, [1], [2], [3]); System.Console.WriteLine($"Name: {p.Name}, Age: {p.Age}, City: {p.City}"); } static void UpdateDetails(Person person, string newName, int newAge, string newCity) { person.Name = newName; person.Age = newAge; person.City = newCity; } } class Person { public string Name { get; set; } public int Age { get; set; } public string City { get; set; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up the order of arguments.
Forgetting quotes around string arguments.
✗ Incorrect
The method call passes name "Denver", age 40, and city "Seattle".