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

Static members vs instance members in C Sharp (C#) - Practice Questions

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Static Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of static and instance members
What is the output of this C# code?
C Sharp (C#)
class Counter {
    public static int count = 0;
    public int instanceCount = 0;

    public Counter() {
        count++;
        instanceCount++;
    }
}

class Program {
    static void Main() {
        Counter a = new Counter();
        Counter b = new Counter();
        System.Console.WriteLine($"Static count: {Counter.count}");
        System.Console.WriteLine($"a.instanceCount: {a.instanceCount}");
        System.Console.WriteLine($"b.instanceCount: {b.instanceCount}");
    }
}
A
Static count: 2
a.instanceCount: 1
b.instanceCount: 1
B
Static count: 1
a.instanceCount: 2
b.instanceCount: 2
C
Static count: 2
a.instanceCount: 2
b.instanceCount: 2
D
Static count: 0
a.instanceCount: 1
b.instanceCount: 1
Attempts:
2 left
💡 Hint
Remember static members are shared across all instances, instance members belong to each object.
🧠 Conceptual
intermediate
1:30remaining
Understanding static member access
Which statement about static members in C# is TRUE?
AStatic members cannot be accessed from static methods.
BStatic members can be accessed without creating an instance of the class.
CStatic members are unique to each instance of the class.
DStatic members can only be accessed through an instance of the class.
Attempts:
2 left
💡 Hint
Think about how you call static methods like Math.Sqrt without creating a Math object.
Predict Output
advanced
2:30remaining
Static constructor behavior
What will be printed when this C# program runs?
C Sharp (C#)
class Example {
    public static int value;

    static Example() {
        value = 10;
        System.Console.WriteLine("Static constructor called");
    }

    public Example() {
        value += 5;
        System.Console.WriteLine("Instance constructor called");
    }
}

class Program {
    static void Main() {
        System.Console.WriteLine(Example.value);
        Example e = new Example();
        System.Console.WriteLine(Example.value);
    }
}
A
Static constructor called
0
Instance constructor called
15
B
0
Instance constructor called
15
C
Static constructor called
10
Instance constructor called
15
D
Static constructor called
10
Instance constructor called
10
Attempts:
2 left
💡 Hint
Static constructors run once before any instance or static member is accessed.
🔧 Debug
advanced
1:30remaining
Why does this code cause an error?
This code tries to access an instance member from a static method. What error will it cause?
C Sharp (C#)
class Sample {
    public int number = 5;

    public static void ShowNumber() {
        System.Console.WriteLine(number);
    }
}
ACompile-time error: An object reference is required for the non-static field 'number'.
BRuntime error: NullReferenceException
CNo error, prints 5
DCompile-time error: Static method cannot be declared in a class
Attempts:
2 left
💡 Hint
Static methods cannot access instance variables directly because they don't belong to an object.
🚀 Application
expert
3:00remaining
Predict the final value of static and instance members
Given the following code, what is the final output?
C Sharp (C#)
class Tracker {
    public static int totalCount = 0;
    public int instanceCount = 0;

    public Tracker() {
        totalCount++;
        instanceCount++;
    }

    public void Increment() {
        totalCount++;
        instanceCount++;
    }
}

class Program {
    static void Main() {
        Tracker t1 = new Tracker();
        Tracker t2 = new Tracker();
        t1.Increment();
        t2.Increment();
        System.Console.WriteLine($"Total count: {Tracker.totalCount}");
        System.Console.WriteLine($"t1 instance count: {t1.instanceCount}");
        System.Console.WriteLine($"t2 instance count: {t2.instanceCount}");
    }
}
A
Total count: 6
t1 instance count: 2
t2 instance count: 2
B
Total count: 4
t1 instance count: 1
t2 instance count: 1
C
Total count: 6
t1 instance count: 1
t2 instance count: 1
D
Total count: 4
t1 instance count: 2
t2 instance count: 2
Attempts:
2 left
💡 Hint
Count how many times static and instance variables are incremented.

Practice

(1/5)
1. Which statement correctly describes a static member in C#?
easy
A. It belongs to the class and is shared by all instances.
B. It belongs to each object and stores unique data.
C. It can only be accessed through an object instance.
D. It is created every time a new object is instantiated.

Solution

  1. Step 1: Understand static member concept

    Static members belong to the class itself, not to any individual object.
  2. Step 2: Compare with instance members

    Instance members belong to objects and hold unique data per object, unlike static members.
  3. Final Answer:

    It belongs to the class and is shared by all instances. -> Option A
  4. Quick Check:

    Static member = shared by class [OK]
Hint: Static = shared by class, instance = unique per object [OK]
Common Mistakes:
  • Confusing static with instance members
  • Thinking static members are unique per object
  • Believing static members require object to access
2. Which of the following is the correct way to declare a static field in a C# class?
easy
A. public static int count;
B. public int static count;
C. public int count;
D. int static public count;

Solution

  1. Step 1: Recall C# static field syntax

    The correct order is access modifier, then 'static', then type and name.
  2. Step 2: Check each option

    public static int count; matches the correct syntax: 'public static int count;'. Others have wrong order or missing keywords.
  3. Final Answer:

    public static int count; -> Option A
  4. Quick Check:

    Access modifier + static + type + name [OK]
Hint: Use 'public static' before type for static fields [OK]
Common Mistakes:
  • Placing 'static' after the type
  • Omitting 'static' keyword
  • Incorrect order of keywords
3. What will be the output of the following C# code?
class Counter {
    public static int count = 0;
    public Counter() {
        count++;
    }
}

class Program {
    static void Main() {
        Counter c1 = new Counter();
        Counter c2 = new Counter();
        Console.WriteLine(Counter.count);
    }
}
medium
A. 0
B. 2
C. 1
D. Compilation error

Solution

  1. Step 1: Understand static field behavior

    The static field 'count' is shared by all instances and starts at 0.
  2. Step 2: Trace constructor calls

    Each new Counter() increments 'count' by 1. Two objects created, so count becomes 2.
  3. Final Answer:

    2 -> Option B
  4. Quick Check:

    Static count increments twice = 2 [OK]
Hint: Static fields keep shared state across all objects [OK]
Common Mistakes:
  • Thinking count resets per object
  • Confusing instance and static fields
  • Expecting compilation error due to static access
4. Identify the error in this C# code snippet:
class Example {
    public static int value = 10;
    public int GetValue() {
        return value;
    }
}
medium
A. Cannot access static field 'value' inside instance method.
B. Method GetValue must be static to access static field 'value'.
C. Static field 'value' must be private.
D. No error; code compiles and runs correctly.

Solution

  1. Step 1: Check static field access rules

    Instance methods can access static fields directly without error.
  2. Step 2: Verify code correctness

    Method GetValue returns static field 'value' correctly; no syntax or access errors.
  3. Final Answer:

    No error; code compiles and runs correctly. -> Option D
  4. Quick Check:

    Instance method can access static field [OK]
Hint: Instance methods can access static fields directly [OK]
Common Mistakes:
  • Thinking instance methods cannot access static fields
  • Believing GetValue must be static
  • Assuming static fields must be private
5. You want to count how many objects of a class are created, but also keep a unique ID for each object starting from 1. Which approach correctly uses static and instance members?
hard
A. Use only instance fields for both counter and ID.
B. Use an instance counter incremented in constructor; assign its value to a static ID field.
C. Use a static counter incremented in constructor; assign its value to an instance ID field.
D. Use static fields for both counter and ID without instance fields.

Solution

  1. Step 1: Understand counting objects with static field

    A static counter shared by all objects can track total created objects.
  2. Step 2: Assign unique ID per object using instance field

    Each object gets its own instance ID assigned from the static counter value.
  3. Final Answer:

    Use a static counter incremented in constructor; assign its value to an instance ID field. -> Option C
  4. Quick Check:

    Static counter + instance ID = unique IDs [OK]
Hint: Static counts total; instance stores unique ID per object [OK]
Common Mistakes:
  • Using instance counter for total count
  • Assigning static ID per object (not unique)
  • Not incrementing counter in constructor