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

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

Choose your learning style9 modes available
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.