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

Methods that operate on state in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
State Mastery in C#
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 C# code that modifies object state?

Consider the following C# class and code snippet. What will be printed to the console?

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

var c = new Counter();
c.Increment();
c.Increment();
Console.WriteLine(c.GetCount());
A0
B1
C2
DCompilation error
Attempts:
2 left
💡 Hint

Think about how many times the Increment method is called before printing.

Predict Output
intermediate
2:00remaining
What does this method return after modifying the object's state?

Given the following C# code, what will be the output?

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

var account = new BankAccount();
account.Withdraw(30m);
Console.WriteLine(account.GetBalance());
A100
B70
C30
DCompilation error
Attempts:
2 left
💡 Hint

Subtract the withdrawn amount from the initial balance.

🔧 Debug
advanced
2:30remaining
Why does this method fail to update the object's state?

Examine the code below. Why does calling Reset not change the value field?

C Sharp (C#)
public class Data {
    private int value = 10;
    public void Reset(int value) {
        value = 0;
    }
    public int GetValue() {
        return value;
    }
}

var d = new Data();
d.Reset(5);
Console.WriteLine(d.GetValue());
AThe method parameter <code>value</code> shadows the field, so the field is not changed.
BThe field <code>value</code> is readonly and cannot be changed.
CThe method <code>Reset</code> is private and cannot be called.
DThe field <code>value</code> is static and must be accessed differently.
Attempts:
2 left
💡 Hint

Look at the parameter name and the field name. Are they the same?

📝 Syntax
advanced
2:00remaining
Which option correctly defines a method that changes object state?

Which of the following method definitions correctly updates the count field in a C# class?

Apublic void Increase() { count++; return count; }
Bpublic void Increase() => count + 1;
Cpublic int Increase() { return count + 1; }
Dpublic void Increase() { count = count + 1; }
Attempts:
2 left
💡 Hint

Remember that methods that change state usually have void return type and update the field directly.

🚀 Application
expert
3:00remaining
How many times is the object's state changed after this sequence?

Given the following C# class and code, how many times is the value field changed?

C Sharp (C#)
public class Tracker {
    private int value = 0;
    public void Update(int newValue) {
        if (newValue != value) {
            value = newValue;
        }
    }
    public int GetValue() => value;
}

var t = new Tracker();
t.Update(5);
t.Update(5);
t.Update(10);
t.Update(10);
t.Update(5);
Console.WriteLine(t.GetValue());
A3
B5
C4
D2
Attempts:
2 left
💡 Hint

Count only when the value field actually changes.