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 Sample {
  public int Number;
  public int NumberProp { get; set; }
}

var s = new Sample();
s.Number = 5;
s.NumberProp = 10;
Console.WriteLine(s.Number + "," + s.NumberProp);
A5,10
B10,5
C0,0
DCompilation error
Step-by-Step Solution
Solution:
  1. Step 1: Understand field and property assignments

    Field Number is assigned 5, property NumberProp is assigned 10.
  2. Step 2: Output values

    Console.WriteLine prints Number and NumberProp separated by a comma, so output is "5,10".
  3. Final Answer:

    5,10 -> Option A
  4. Quick Check:

    Field and property values print as assigned [OK]
Quick Trick: Fields and properties hold assigned values independently [OK]
Common Mistakes:
MISTAKES
  • Confusing order of output
  • Expecting default values instead of assigned
  • Thinking code causes error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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