Bird
0
0

You want to create a property Age that only accepts values between 0 and 120 inclusive. Which setter implementation correctly enforces this?

hard🚀 Application Q8 of 15
C Sharp (C#) - Properties and Encapsulation
You want to create a property Age that only accepts values between 0 and 120 inclusive. Which setter implementation correctly enforces this?
Aset { if (value < 0 && value > 120) throw new ArgumentOutOfRangeException(); age = value; }
Bset { if (value <= 0 && value >= 120) throw new ArgumentOutOfRangeException(); age = value; }
Cset { if (value < 0 || value > 120) throw new ArgumentOutOfRangeException(); age = value; }
Dset { if (value > 0 || value < 120) throw new ArgumentOutOfRangeException(); age = value; }
Step-by-Step Solution
Solution:
  1. Step 1: Understand the valid range condition

    Age must be between 0 and 120 inclusive, so invalid if less than 0 or greater than 120.
  2. Step 2: Check logical operators in conditions

    set { if (value < 0 || value > 120) throw new ArgumentOutOfRangeException(); age = value; } uses '||' (or) correctly to check if value is outside the range.
  3. Final Answer:

    set { if (value < 0 || value > 120) throw new ArgumentOutOfRangeException(); age = value; } -> Option C
  4. Quick Check:

    Use '||' to check out-of-range values [OK]
Quick Trick: Use OR (||) to check if value is outside range [OK]
Common Mistakes:
MISTAKES
  • Using AND (&&) instead of OR (||) for range checks
  • Reversing comparison operators
  • Missing inclusive range boundaries

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Sharp (C#) Quizzes