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

Property validation logic in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Property Validation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
    }
}
A25\nInvalid age
BInvalid age\n0
CInvalid age\n25
D25\nInvalid age\n25
Attempts:
2 left
💡 Hint
Think about when the age field is updated and when the message is printed.
Predict Output
intermediate
2: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;
        }
    }
}
ANullReferenceException at runtime
BStackOverflowException at runtime
CNo error, runs fine
DCompile-time error: missing semicolon
Attempts:
2 left
💡 Hint
Look at how the setter assigns the value inside itself.
🧠 Conceptual
advanced
2: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?
AUse a private field with lock statements in both getter and setter
BUse only a public auto-property with no validation
CValidate only in the constructor and make property read-only
DUse a static property with validation logic
Attempts:
2 left
💡 Hint
Think about how to prevent race conditions when multiple threads access the property.
🔧 Debug
advanced
2: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;
        }
    }
}
AThe private field is not initialized
BThe setter is missing a return statement
CThe if condition checks the old value instead of the new value
DThe property should be static
Attempts:
2 left
💡 Hint
Check what variable the if condition is testing.
🚀 Application
expert
3: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);
    }
}
A2
B3
C4
D1
Attempts:
2 left
💡 Hint
Consider how dictionary keys behave when assigned multiple times.