Bird
Raised Fist0

Given class Point with constructor Point(int x, int y), how do you create an array of 2 points at (1,2) and (3,4)?

hard🚀 Application Q9 of Q15
C Sharp (C#) - Classes and Objects
Given class Point with constructor Point(int x, int y), how do you create an array of 2 points at (1,2) and (3,4)?
APoint[] points = { Point(1, 2), Point(3, 4) };
BPoint[] points = new Point[2] { new Point(1, 2), new Point(3, 4) };
CPoint[] points = new Point[2]; points[0] = Point(1, 2); points[1] = Point(3, 4);
DPoint[] points = new Point[] { Point(1, 2), Point(3, 4) };
Step-by-Step Solution
Solution:
  1. Step 1: Understand array initialization with objects

    When specifying size and initializer, syntax requires new Type[size] { ... }.
  2. Step 2: Check each option

    Point[] points = new Point[2] { new Point(1, 2), new Point(3, 4) }; correctly declares array size and initializes with new Point objects. Point[] points = { Point(1, 2), Point(3, 4) }; misses new keyword for objects, B misses new keyword for objects, C misses new keyword for objects.
  3. Final Answer:

    Point[] points = new Point[2] { new Point(1, 2), new Point(3, 4) }; -> Option B
  4. Quick Check:

    Array with size and initializer needs new Type[size] { ... } [OK]
Quick Trick: Use new Type[size] { new Type(args), ... } for arrays [OK]
Common Mistakes:
MISTAKES
  • Omitting new keyword for array or objects
  • Assigning objects without new keyword
  • Mismatching array size and initializer

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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