Bird
Raised Fist0

What will be the output of this code?

medium📝 Predict Output Q5 of Q15
C Sharp (C#) - Classes and Objects
What will be the output of this code?
class Point {
  public int x, y;
  public Point() { x = 0; y = 0; }
  public Point(int a) { x = a; y = a; }
  public Point(int a, int b) { x = a; y = b; }
}

var pt = new Point(5);
Console.WriteLine(pt.x + ","+ pt.y);
A5,5
B0,0
C5,0
DCompilation error
Step-by-Step Solution
Solution:
  1. Step 1: Identify constructor called

    Point(5) calls constructor with one int parameter, setting x and y to 5.
  2. Step 2: Determine output values

    pt.x = 5 and pt.y = 5, so output is "5,5".
  3. Final Answer:

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

    Single parameter constructor sets both x and y [OK]
Quick Trick: Single int parameter sets both coordinates [OK]
Common Mistakes:
MISTAKES
  • Assuming default constructor is called
  • Mixing parameter counts
  • Expecting compilation error due to multiple constructors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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