0
0
C Sharp (C#)programming~10 mins

Struct declaration and behavior in C Sharp (C#) - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Aclass
Benum
Cstruct
Dinterface
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'class' instead of 'struct' will create a reference type, not a value type.
2fill in blank
medium

Complete 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'
A5
B10
C0
D15
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the values of X and Y.
Using zero instead of 5 for X.
3fill in blank
hard

Fix 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'
Ap1
Bnull
Cnew Point()
Ddefault
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.
4fill in blank
hard

Fill 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'
ASqrt
BDistance
CLength
DMagnitude
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.
5fill in blank
hard

Fill 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'
AX
Bdx
Cdy
DY
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up field names and parameter names.
Adding dy to X instead of dx.