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

Instance fields and state in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Instance Fields Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this code with instance fields?

Consider the following C# class and code. What will be printed when the program runs?

C Sharp (C#)
class Counter {
    private int count = 0;
    public void Increment() {
        count++;
    }
    public int GetCount() {
        return count;
    }
}

var c1 = new Counter();
c1.Increment();
c1.Increment();
var c2 = new Counter();
c2.Increment();
Console.WriteLine(c1.GetCount());
Console.WriteLine(c2.GetCount());
A3\n1
B1\n1
C2\n1
D2\n2
Attempts:
2 left
💡 Hint

Remember each object has its own copy of instance fields.

Predict Output
intermediate
2:00remaining
What is the value of the instance field after method calls?

What will be the value of balance after running this code?

C Sharp (C#)
class BankAccount {
    private decimal balance = 100m;
    public void Deposit(decimal amount) {
        balance += amount;
    }
    public void Withdraw(decimal amount) {
        balance -= amount;
    }
    public decimal GetBalance() {
        return balance;
    }
}

var account = new BankAccount();
account.Deposit(50m);
account.Withdraw(30m);
var result = account.GetBalance();
Console.WriteLine(result);
A80
B120
C150
D100
Attempts:
2 left
💡 Hint

Think about adding and subtracting from the starting balance.

🔧 Debug
advanced
2:30remaining
Why does this instance field not update as expected?

Look at this code. Why does the count field not increase after calling Increment?

C Sharp (C#)
class Counter {
    private int count = 0;
    public void Increment() {
        int count = 0;
        count++;
    }
    public int GetCount() {
        return count;
    }
}

var c = new Counter();
c.Increment();
c.Increment();
Console.WriteLine(c.GetCount());
AThe instance field 'count' is reset to zero every time Increment is called.
BThe instance field 'count' is private and cannot be changed outside the class.
CThe Increment method does not have a return statement, so count is not updated.
DThe local variable 'count' inside Increment hides the instance field, so the instance field never changes.
Attempts:
2 left
💡 Hint

Check variable names inside the method and the class.

📝 Syntax
advanced
1:30remaining
Which option correctly declares and initializes an instance field?

Which of the following options correctly declares an instance field name of type string and initializes it to "Alice"?

Aprivate string name = "Alice";
B
string name;
name = "Alice";
C
private string name;
name = "Alice";
Dstring name = "Alice";
Attempts:
2 left
💡 Hint

Instance fields are declared inside the class but outside methods, with optional access modifiers and initialization.

🚀 Application
expert
3:00remaining
How many unique instance states exist after this code runs?

Given the class and code below, how many unique Person objects have different age values after execution?

C Sharp (C#)
class Person {
    public int age;
    public Person(int age) {
        this.age = age;
    }
}

var p1 = new Person(20);
var p2 = new Person(25);
var p3 = p1;
p3.age = 30;
var p4 = new Person(30);
var p5 = p2;
p5.age = 35;

var list = new List<Person> { p1, p2, p3, p4, p5 };
var uniqueAges = list.Select(p => p.age).Distinct().Count();
Console.WriteLine(uniqueAges);
A2
B4
C3
D5
Attempts:
2 left
💡 Hint

Remember that p3 and p1 refer to the same object, as do p5 and p2.