Bird
0
0

What will be the output of this C# code?

medium📝 Predict Output Q5 of 15
C Sharp (C#) - Classes and Objects
What will be the output of this C# code?
class Point {
  public int X, Y;
  public Point() {
    X = 5;
    Y = 10;
  }
}
class Program {
  static void Main() {
    Point p = new Point();
    System.Console.WriteLine($"{p.X}, {p.Y}");
  }
}
A0, 0
B5, 10
CCompilation error
D5, 0
Step-by-Step Solution
Solution:
  1. Step 1: Review constructor assignments

    Constructor sets X=5 and Y=10 when Point is created.
  2. Step 2: Output values printed

    Console prints "5, 10" from the object's fields.
  3. Final Answer:

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

    Constructor initializes fields = A [OK]
Quick Trick: Constructor sets fields; output matches those values. [OK]
Common Mistakes:
MISTAKES
  • Assuming default zero values instead of assigned
  • Expecting compilation error due to missing parameters
  • Mixing up field values in output

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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