Bird
Raised Fist0

What will be the output of the following C# code?

medium📝 Predict Output Q13 of Q15
C Sharp (C#) - Properties and Encapsulation
What will be the output of the following C# code?
class Person {
  private string name;
  public string Name {
    get { return name; }
    set { name = value.ToUpper(); }
  }
}

var p = new Person();
p.Name = "alice";
Console.WriteLine(p.Name);
Aalice
BALICE
CName
Dnull
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the set accessor behavior

    The set accessor converts the assigned value to uppercase before storing it in the private field.
  2. Step 2: Analyze the get accessor output

    The get accessor returns the stored uppercase string.
  3. Final Answer:

    ALICE -> Option B
  4. Quick Check:

    Set converts to uppercase, output = ALICE [OK]
Quick Trick: Set modifies value before storing, get returns stored value [OK]
Common Mistakes:
MISTAKES
  • Ignoring the ToUpper() call in set
  • Assuming original case is preserved
  • Confusing field and property names

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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