Bird
0
0

What will be the output of this C# code?

medium📝 Predict Output Q4 of 15
C Sharp (C#) - Properties and Encapsulation

What will be the output of this C# code?

class Person {
    private string _name = "Alice";
    public string Name { get => _name; }
}

var p = new Person();
Console.WriteLine(p.Name);
AAlice
Bnull
CCompilation error
DRuntime error
Step-by-Step Solution
Solution:
  1. Step 1: Analyze property and field

    The property Name returns the private field _name which is initialized to "Alice".
  2. Step 2: Understand output of Console.WriteLine

    Printing p.Name outputs the string "Alice".
  3. Final Answer:

    Alice -> Option A
  4. Quick Check:

    Read-only property returns value correctly = Alice [OK]
Quick Trick: Read-only property returns stored value when accessed [OK]
Common Mistakes:
MISTAKES
  • Expecting error due to missing set
  • Thinking private field is inaccessible
  • Confusing output with null

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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