Bird
Raised Fist0
C Sharp (C#)programming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. Which of the following statements about struct in C# is true?
easy
A. Structs support inheritance from other structs.
B. Structs are value types and are copied when assigned to a new variable.
C. Structs can have a parameterless constructor defined by the user.
D. Structs are reference types and stored on the heap.

Solution

  1. Step 1: Understand struct type behavior

    Structs in C# are value types, meaning when you assign them to another variable, a copy is made.
  2. Step 2: Check inheritance and constructors rules

    Structs cannot inherit from other structs or classes, and they cannot have user-defined parameterless constructors. They are stored on the stack, not the heap.
  3. Final Answer:

    Structs are value types and are copied when assigned to a new variable. -> Option B
  4. Quick Check:

    Struct = Value type copy [OK]
Hint: Remember: structs copy values, no inheritance, no default constructor [OK]
Common Mistakes:
  • Thinking structs are reference types
  • Assuming structs support inheritance
  • Trying to define parameterless constructors in structs
2. Which of the following is the correct way to declare a struct named Point with two integer fields x and y?
easy
A. struct Point { public int x; public int y; }
B. class Point { int x; int y; }
C. struct Point { int x; int y; }
D. struct Point { int x; int y; void Point() {} }

Solution

  1. Step 1: Check struct field accessibility

    Fields inside a struct should be declared with an access modifier like public to be accessible outside.
  2. Step 2: Validate struct syntax

    struct Point { int x; int y; } misses access modifiers, so fields are private by default and not accessible. class Point { int x; int y; } uses class, not struct. struct Point { int x; int y; void Point() {} } tries to define a constructor with void return type, which is invalid.
  3. Final Answer:

    struct Point { public int x; public int y; } -> Option A
  4. Quick Check:

    Struct fields need public modifier [OK]
Hint: Use public fields in structs for external access [OK]
Common Mistakes:
  • Omitting access modifiers on struct fields
  • Confusing struct with class keyword
  • Defining constructors with void return type
3. What is the output of the following C# code?
struct Point { public int X; public int Y; }
Point p1 = new Point { X = 5, Y = 10 };
Point p2 = p1;
p2.X = 20;
Console.WriteLine(p1.X);
medium
A. 20
B. 10
C. 5
D. Compilation error

Solution

  1. Step 1: Understand struct assignment behavior

    Assigning p1 to p2 copies the values because structs are value types.
  2. Step 2: Analyze modification effect

    Changing p2.X to 20 does not affect p1.X, which remains 5.
  3. Final Answer:

    5 -> Option C
  4. Quick Check:

    Struct copy means original unchanged [OK]
Hint: Struct assignment copies values, original stays same [OK]
Common Mistakes:
  • Assuming structs behave like reference types
  • Expecting p1.X to change after modifying p2.X
  • Confusing output with Y field value
4. Identify the error in the following struct declaration:
struct Rectangle {
public int Width;
public int Height;
public Rectangle() { Width = 0; Height = 0; }
}
medium
A. Structs must inherit from System.ValueType explicitly.
B. Structs cannot have any constructors.
C. Fields must be private in structs.
D. Parameterless constructors are not allowed in structs.

Solution

  1. Step 1: Recall struct constructor rules

    Structs cannot have user-defined parameterless constructors; the compiler provides a default one.
  2. Step 2: Check other options

    Structs can have constructors with parameters, fields can be public, and inheritance from System.ValueType is implicit.
  3. Final Answer:

    Parameterless constructors are not allowed in structs. -> Option D
  4. Quick Check:

    No user parameterless constructor in struct [OK]
Hint: Structs get default parameterless constructor automatically [OK]
Common Mistakes:
  • Trying to define parameterless constructor in struct
  • Thinking structs must inherit explicitly
  • Believing fields must be private
5. You want to create a struct Color that stores red, green, and blue values as bytes. You also want to ensure that when you assign one Color variable to another, changes to the second do not affect the first. Which approach is correct?
hard
A. Declare Color as a struct with public byte fields and assign normally.
B. Declare Color as a class with public byte fields and assign normally.
C. Declare Color as a struct but use pointers to share data.
D. Declare Color as a class and override the assignment operator.

Solution

  1. Step 1: Understand value vs reference types

    Structs are value types, so assigning one struct variable to another copies the data, preventing shared changes.
  2. Step 2: Evaluate options for data isolation

    Using a class would create reference types, so changes to one variable affect the other. Using pointers or overriding assignment is unnecessary and complex.
  3. Final Answer:

    Declare Color as a struct with public byte fields and assign normally. -> Option A
  4. Quick Check:

    Struct assignment copies data, no shared changes [OK]
Hint: Use struct for independent copies of small data [OK]
Common Mistakes:
  • Using class instead of struct for value copy
  • Trying to share data with pointers unnecessarily
  • Overriding assignment operator in class (not allowed)
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard