Bird
0
0

You want to create a computed property IsAdult in a Person class that returns true if Age is 18 or more, otherwise false. Which code correctly implements this?

hard🚀 Application Q15 of 15
C Sharp (C#) - Properties and Encapsulation
You want to create a computed property IsAdult in a Person class that returns true if Age is 18 or more, otherwise false. Which code correctly implements this?
Apublic bool IsAdult() { return Age >= 18; }
Bpublic bool IsAdult { get { return Age > 18; } }
Cpublic bool IsAdult { get; set; } = Age >= 18;
Dpublic bool IsAdult => Age >= 18;
Step-by-Step Solution
Solution:
  1. Step 1: Understand requirement for computed property

    IsAdult should return true if Age is 18 or more, false otherwise, without storing a value.
  2. Step 2: Check each option

    public bool IsAdult => Age >= 18; uses expression-bodied property correctly with >= 18. public bool IsAdult { get { return Age > 18; } } uses > 18 (wrong condition). public bool IsAdult { get; set; } = Age >= 18; tries to set property with Age comparison, which is invalid. public bool IsAdult() { return Age >= 18; } is a method, not a property.
  3. Final Answer:

    public bool IsAdult => Age >= 18; -> Option D
  4. Quick Check:

    Age >= 18 for IsAdult [OK]
Quick Trick: Use => with condition for simple computed properties [OK]
Common Mistakes:
MISTAKES
  • Using > instead of >= for age check
  • Trying to set computed property value
  • Confusing methods with properties

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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