Challenge - 5 Problems
Property Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this property validation code?
Consider the following C# class with a property that validates input. What will be printed when the Main method runs?
C Sharp (C#)
using System; class Person { private int age; public int Age { get => age; set { if (value < 0 || value > 120) Console.WriteLine("Invalid age"); else age = value; } } } class Program { static void Main() { Person p = new Person(); p.Age = 25; Console.WriteLine(p.Age); p.Age = -5; Console.WriteLine(p.Age); } }
Attempts:
2 left
💡 Hint
Think about when the age field is updated and when the message is printed.
✗ Incorrect
The setter only updates the private field age if the value is valid. When setting -5, it prints "Invalid age" but does not change age. So the last printed age remains 25.
❓ Predict Output
intermediate2:00remaining
What error does this property validation code produce?
Examine this C# class with a property setter. What error will occur when compiling or running this code?
C Sharp (C#)
using System; class Product { private decimal price; public decimal Price { get { return price; } set { if (value < 0) throw new ArgumentException("Price cannot be negative"); else Price = value; } } }
Attempts:
2 left
💡 Hint
Look at how the setter assigns the value inside itself.
✗ Incorrect
The setter calls Price = value; which calls the setter again, causing infinite recursion and a StackOverflowException at runtime.
🧠 Conceptual
advanced2:00remaining
Which property validation approach ensures thread safety?
You want to validate a property value and ensure that multiple threads can safely set and get it without causing inconsistent state. Which approach below best achieves this?
Attempts:
2 left
💡 Hint
Think about how to prevent race conditions when multiple threads access the property.
✗ Incorrect
Using a private field with lock in getter and setter ensures that only one thread accesses the field at a time, preventing inconsistent state.
🔧 Debug
advanced2:00remaining
Why does this property validation code fail to reject invalid input?
This C# class tries to reject negative values for the Score property, but it still allows negative values. Why?
C Sharp (C#)
using System; class Game { private int score; public int Score { get => score; set { if (value < 0) throw new ArgumentException("Score cannot be negative"); score = value; } } }
Attempts:
2 left
💡 Hint
Check what variable the if condition is testing.
✗ Incorrect
The condition if (score < 0) checks the current field value, not the new value value. So negative inputs are not caught.
🚀 Application
expert3:00remaining
How many items are in the dictionary after this property validation code runs?
This C# class uses a property with validation to add entries to a dictionary. After running the Main method, how many items are in the dictionary?
C Sharp (C#)
using System; using System.Collections.Generic; class Registry { private Dictionary<string, int> data = new(); private int value; public int Value { get => value; set { if (value >= 0) { this.value = value; data[$"key{value}"] = value; } } } public int Count => data.Count; } class Program { static void Main() { Registry r = new Registry(); r.Value = 1; r.Value = 2; r.Value = -1; r.Value = 2; Console.WriteLine(r.Count); } }
Attempts:
2 left
💡 Hint
Consider how dictionary keys behave when assigned multiple times.
✗ Incorrect
The dictionary keys are "key1" and "key2" after valid assignments. The negative value is ignored. Assigning "key2" twice overwrites the same entry, so total count is 2.