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

Record declaration syntax in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Record Syntax Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of a simple record declaration
What is the output of this C# program using a record declaration?
C Sharp (C#)
public record Person(string Name, int Age);

class Program {
    static void Main() {
        var p = new Person("Alice", 30);
        System.Console.WriteLine(p);
    }
}
AName: Alice, Age: 30
BAlice 30
CPerson(Alice, 30)
DPerson { Name = Alice, Age = 30 }
Attempts:
2 left
💡 Hint
Records override ToString() to show property names and values.
❓ Predict Output
intermediate
2:00remaining
Output of record with inheritance
What is the output of this C# program with record inheritance?
C Sharp (C#)
public record Animal(string Name);
public record Dog(string Name, string Breed) : Animal(Name);

class Program {
    static void Main() {
        var d = new Dog("Buddy", "Beagle");
        System.Console.WriteLine(d);
    }
}
ADog { Name = Buddy, Breed = Beagle }
BAnimal { Name = Buddy }
CDog(Buddy, Beagle)
DBuddy Beagle
Attempts:
2 left
💡 Hint
Derived records include all properties in ToString output.
🔧 Debug
advanced
2:00remaining
Identify the syntax error in record declaration
Which option contains a syntax error in the record declaration?
Apublic record Car(string Make; string Model);
Bpublic record Car(string Make, string Model);
Cpublic record Car(string Make, string Model) {}
Dpublic record Car { string Make; string Model; }
Attempts:
2 left
💡 Hint
Check the separator between parameters in the record declaration.
❓ Predict Output
advanced
2:00remaining
Output of record with custom ToString override
What is the output of this C# program with a record that overrides ToString()?
C Sharp (C#)
public record Point(int X, int Y) {
    public override string ToString() => $"({X}, {Y})";
}

class Program {
    static void Main() {
        var pt = new Point(3, 4);
        System.Console.WriteLine(pt);
    }
}
A3, 4
BPoint { X = 3, Y = 4 }
C(3, 4)
DPoint(3, 4)
Attempts:
2 left
💡 Hint
The record overrides ToString to return a custom string.
🧠 Conceptual
expert
2:00remaining
Number of properties in a record with inheritance
Given these record declarations, how many properties does the record 'Employee' have?
C Sharp (C#)
public record Person(string Name, int Age);
public record Employee(string Name, int Age, string Position) : Person(Name, Age);
A2
B3
C4
D1
Attempts:
2 left
💡 Hint
Count all positional parameters from base and derived records.

Practice

(1/5)
1. What is a key feature of a record in C#?
easy
A. It provides immutable data with value-based equality.
B. It allows mutable fields by default.
C. It is used only for database connections.
D. It cannot have properties.

Solution

  1. Step 1: Understand what a record is

    A record is a special type in C# designed to hold data immutably.
  2. Step 2: Identify the main feature

    Records compare by value, not by reference, and are immutable by default.
  3. Final Answer:

    It provides immutable data with value-based equality. -> Option A
  4. Quick Check:

    Record = Immutable + Value Equality [OK]
Hint: Remember: records are immutable and compare by value [OK]
Common Mistakes:
  • Thinking records are mutable by default
  • Confusing records with classes for database use
  • Believing records cannot have properties
2. Which of the following is the correct syntax to declare a record with two properties int Id and string Name?
easy
A. record Person { int Id; string Name; }
B. record Person(int Id, string Name);
C. record Person = (int Id, string Name);
D. record Person(int Id; string Name);

Solution

  1. Step 1: Recall record declaration syntax

    Records use parentheses with comma-separated parameters for properties.
  2. Step 2: Check each option

    record Person(int Id, string Name); uses correct syntax: parentheses and commas. record Person { int Id; string Name; } uses braces without property syntax. record Person = (int Id, string Name); uses '=' incorrectly. record Person(int Id; string Name); uses semicolon instead of comma.
  3. Final Answer:

    record Person(int Id, string Name); -> Option B
  4. Quick Check:

    Record syntax = parentheses + commas [OK]
Hint: Use parentheses with commas for record properties [OK]
Common Mistakes:
  • Using braces instead of parentheses
  • Using semicolons instead of commas
  • Adding '=' sign incorrectly
3. What will be the output of the following code?
record Point(int X, int Y);
var p1 = new Point(3, 4);
var p2 = new Point(3, 4);
Console.WriteLine(p1 == p2);
medium
A. True
B. False
C. Compilation error
D. Runtime exception

Solution

  1. Step 1: Understand record equality behavior

    Records compare by value, so two records with same property values are equal.
  2. Step 2: Analyze the code

    p1 and p2 have same X and Y values (3,4), so p1 == p2 returns true.
  3. Final Answer:

    True -> Option A
  4. Quick Check:

    Records compare by value = True [OK]
Hint: Records compare by value, so identical values mean equality [OK]
Common Mistakes:
  • Assuming reference equality instead of value equality
  • Expecting false because of different instances
  • Thinking it causes a compilation error
4. Identify the error in this record declaration:
record Car(string Make, string Model)
{
public int Year;
}
medium
A. Records cannot have any fields declared inside braces.
B. The semicolon is missing after the record declaration.
C. The record must use parentheses for properties, not braces.
D. Fields inside records must be declared as properties, not fields.

Solution

  1. Step 1: Understand record members

    Records can have members, but fields should be properties for immutability.
  2. Step 2: Analyze the code

    The code declares a public field 'Year' instead of a property, which is not recommended and causes issues.
  3. Final Answer:

    Fields inside records must be declared as properties, not fields. -> Option D
  4. Quick Check:

    Use properties, not fields inside records [OK]
Hint: Use properties, not fields, inside records for immutability [OK]
Common Mistakes:
  • Using fields instead of properties inside records
  • Missing semicolon after record declaration (not required here)
  • Confusing braces and parentheses usage
5. You want to declare a record Book with properties string Title, string Author, and an optional int? Year that defaults to null. Which is the correct way to declare it?
hard
A. record Book { string Title; string Author; int? Year = null; }
B. record Book(string Title, string Author, int? Year);
C. record Book(string Title, string Author, int? Year = null);
D. record Book(string Title, string Author) { public int? Year = null; }

Solution

  1. Step 1: Use concise record syntax with default parameter

    Records allow default values in the parameter list using '='.
  2. Step 2: Check each option

    record Book(string Title, string Author, int? Year = null); correctly declares Year as nullable with default null. record Book(string Title, string Author, int? Year); lacks default. record Book { string Title; string Author; int? Year = null; } uses invalid field syntax. record Book(string Title, string Author) { public int? Year = null; } uses field with default inside braces, which is not idiomatic.
  3. Final Answer:

    record Book(string Title, string Author, int? Year = null); -> Option C
  4. Quick Check:

    Default values go in parentheses parameters [OK]
Hint: Set default values inside parentheses in record declarations [OK]
Common Mistakes:
  • Omitting default value for optional parameters
  • Using fields instead of properties inside braces
  • Trying to assign default values inside braces
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