Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a struct named Point.
C Sharp (C#)
public [1] Point { public int X; public int Y; } Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'class' instead of 'struct' will create a reference type, not a value type.
✗ Incorrect
The keyword struct is used to declare a value type in C#.
2fill in blank
mediumComplete the code to create a new Point struct instance with X=5 and Y=10.
C Sharp (C#)
Point p = new Point() { X = [1], Y = 10 }; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the values of X and Y.
Using zero instead of 5 for X.
✗ Incorrect
We assign X the value 5 as required.
3fill in blank
hardFix the error in the code to correctly assign one Point to another.
C Sharp (C#)
Point p1 = new Point() { X = 1, Y = 2 };
Point p2;
p2 = [1]; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning null to a struct variable causes an error.
Using 'new Point()' creates a new default struct, not a copy.
✗ Incorrect
Assigning p1 to p2 copies the struct values.
4fill in blank
hardFill both blanks to complete the method that returns the distance from origin.
C Sharp (C#)
public double Distance() {
return Math.[1](X * X + Y * Y);
}
// Usage:
Point p = new Point() { X = 3, Y = 4 };
double dist = p.[2](); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect math functions like Distance or Length which don't exist in Math class.
Calling a method name that doesn't match the declared method.
✗ Incorrect
The method Math.Sqrt calculates the square root. The struct method is called Distance, and usage calls the method name, which is the same here.
5fill in blank
hardFill all three blanks to complete the struct with a constructor and a method that moves the point.
C Sharp (C#)
public struct Point {
public int X;
public int Y;
public Point(int x, int y) {
[1] = x;
[2] = y;
}
public void Move(int dx, int dy) {
X += [3];
Y += dy;
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up field names and parameter names.
Adding dy to X instead of dx.
✗ Incorrect
The constructor assigns parameters to fields X and Y. The Move method adds dx to X and dy to Y.