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

Struct declaration and behavior in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Struct Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of struct value assignment

What is the output of this C# code?

C Sharp (C#)
struct Point {
    public int X;
    public int Y;
}

class Program {
    static void Main() {
        Point p1 = new Point { X = 5, Y = 10 };
        Point p2 = p1;
        p2.X = 20;
        System.Console.WriteLine(p1.X);
    }
}
A20
B5
C0
DCompilation error
Attempts:
2 left
💡 Hint

Remember that structs are value types and copying creates a new copy.

Predict Output
intermediate
2:00remaining
Struct method modifying fields

What is the output of this C# program?

C Sharp (C#)
struct Counter {
    public int Count;
    public void Increment() {
        Count++;
    }
}

class Program {
    static void Main() {
        Counter c = new Counter();
        c.Increment();
        System.Console.WriteLine(c.Count);
    }
}
A1
BRuntime error
CCompilation error
D0
Attempts:
2 left
💡 Hint

Instance methods on structs operate on a copy of the struct.

Predict Output
advanced
2:00remaining
Struct boxing and method call effect

What is the output of this C# code?

C Sharp (C#)
struct Number {
    public int Value;
    public void DoubleValue() {
        Value *= 2;
    }
}

class Program {
    static void Main() {
        Number n = new Number { Value = 10 };
        object o = n;
        ((Number)o).DoubleValue();
        System.Console.WriteLine(n.Value);
    }
}
ACompilation error
B20
C10
DRuntime error
Attempts:
2 left
💡 Hint

Think about what happens when a struct is boxed.

Predict Output
advanced
2:00remaining
Struct default constructor behavior

What is the output of this C# program?

C Sharp (C#)
struct Data {
    public int Number;
    public string Text;
}

class Program {
    static void Main() {
        Data d = new Data();
        System.Console.WriteLine($"{d.Number}, {d.Text ?? "null"}");
    }
}
A0, null
B0, ""
CCompilation error
DRuntime error
Attempts:
2 left
💡 Hint

Struct fields get default values when using the default constructor.

🧠 Conceptual
expert
3:00remaining
Struct immutability and method behavior

Consider this struct and code snippet. Which statement is true about the output?

struct ImmutablePoint {
    public int X { get; init; }
    public int Y { get; init; }
    public ImmutablePoint(int x, int y) {
        X = x;
        Y = y;
    }
    public ImmutablePoint Move(int dx, int dy) {
        return new ImmutablePoint(X + dx, Y + dy);
    }
}

class Program {
    static void Main() {
        ImmutablePoint p = new ImmutablePoint(1, 2);
        p.Move(3, 4);
        System.Console.WriteLine($"{p.X}, {p.Y}");
    }
}
ARuntime error due to uninitialized properties.
BThe output is '4, 6' because Move modifies the struct fields directly.
CCompilation error because properties have no setters.
DThe output is '1, 2' because structs are immutable and Move returns a new struct without changing p.
Attempts:
2 left
💡 Hint

Think about how immutable structs behave and what the Move method returns.