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
Recall & Review
beginner
What is an instance field in C#?
An instance field is a variable declared inside a class but outside any method. Each object (instance) of the class has its own copy of this variable, which holds the object's state.
Click to reveal answer
beginner
How does an instance field differ from a static field?
An instance field belongs to each object separately, so each object has its own value. A static field belongs to the class itself and is shared by all objects of that class.
Click to reveal answer
beginner
Why do instance fields represent the state of an object?
Because instance fields store data unique to each object, they describe the current condition or characteristics of that object, which is called its state.
Click to reveal answer
beginner
Consider this code snippet:<br><pre>class Car { public string color; }</pre><br>What does the 'color' field represent?
The 'color' field is an instance field that stores the color of each Car object. Each Car can have a different color stored in its own 'color' field.
Click to reveal answer
beginner
How do you access an instance field in C#?
You access an instance field through an object of the class using the dot (.) operator. For example: <code>myObject.fieldName</code>.
Click to reveal answer
What does an instance field store in C#?
AData unique to each object
BData shared by all objects
COnly constant values
DTemporary method variables
✗ Incorrect
Instance fields hold data unique to each object, representing its state.
How do you declare an instance field in a class?
AInside a method
BUsing the static keyword
COutside methods but inside the class
DOnly in interfaces
✗ Incorrect
Instance fields are declared inside the class but outside any method.
Which keyword makes a field shared by all objects of a class?
Ashared
Bstatic
Cinstance
Dpublic
✗ Incorrect
The 'static' keyword makes a field shared by all instances of the class.
If you create two objects of a class, how many copies of an instance field exist?
ATwo copies, one per object
BNone
COne copy shared by both
DDepends on the field type
✗ Incorrect
Each object has its own copy of instance fields.
How do you access an instance field named 'age' of an object 'person'?
Aage.person()
Bage.person
Cperson->age
Dperson.age
✗ Incorrect
Use the dot operator: objectName.fieldName, so person.age.
Explain what instance fields are and how they relate to an object's state.
Think about how each object remembers its own information.
You got /4 concepts.
Describe the difference between instance fields and static fields in C#.
Consider who owns the data: the object or the class.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of an instance field in a C# class?
easy
A. To store data unique to each object created from the class
B. To define a method that all objects share
C. To create a temporary variable inside a method
D. To hold data shared by all objects of the class
Solution
Step 1: Understand what instance fields represent
Instance fields hold data that belongs to each individual object, not shared across all objects.
Step 2: Differentiate from static fields and methods
Static fields hold shared data, methods define behavior, and local variables are temporary inside methods.
Final Answer:
To store data unique to each object created from the class -> Option A
Quick Check:
Instance field = unique object data [OK]
Hint: Instance fields hold unique data per object, not shared [OK]
Common Mistakes:
Confusing instance fields with static fields
Thinking methods are instance fields
Mixing local variables with instance fields
2. Which of the following is the correct way to declare an instance field named count of type int inside a C# class?
easy
A. static int count;
B. int count() {}
C. void count;
D. int count;
Solution
Step 1: Identify correct syntax for instance field declaration
Instance fields are declared with a type and name, without static keyword or parentheses.
Step 2: Check each option
static int count; is static, not instance. void count; uses void which is invalid for fields. int count() {} looks like a method, not a field.
Final Answer:
int count; -> Option D
Quick Check:
Instance field syntax = type + name [OK]
Hint: Instance fields: type and name, no parentheses or static [OK]
Common Mistakes:
Using static keyword for instance fields
Adding parentheses like a method
Using void as a type for fields
3. What will be the output of this C# code?
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());
medium
A. 0
B. 2
C. 1
D. Compilation error
Solution
Step 1: Trace the method calls on the object
The object c calls Increment() twice, each increasing count by 1.
Step 2: Check the value returned by GetCount()
After two increments, count is 2, so GetCount() returns 2.
Final Answer:
2 -> Option B
Quick Check:
2 increments = count 2 [OK]
Hint: Each Increment adds 1; two calls mean count is 2 [OK]
Common Mistakes:
Forgetting that count starts at 0
Assuming Increment does not change count
Confusing method return types
4. Identify the error in this C# class that tries to track a score:
class Game {
int score;
public void AddPoints(int points) {
score = score + points;
}
public int GetScore() {
return score;
}
}
var g = new Game();
g.AddPoints(5);
Console.WriteLine(g.GetScore());
medium
A. No error; code runs and prints 5
B. score should be declared static
C. AddPoints method should return int
D. score is not initialized and may have a default value
Solution
Step 1: Check field initialization rules in C#
Instance fields like score default to 0 if not explicitly initialized.
Step 2: Verify method behavior and output
AddPoints adds points correctly, and GetScore returns the updated score. The code prints 5 as expected.
Final Answer:
No error; code runs and prints 5 -> Option A
Quick Check:
Uninitialized int defaults to 0 in C# [OK]
Hint: Instance int fields default to 0 if not set [OK]
Common Mistakes:
Thinking uninitialized int fields cause errors
Believing AddPoints must return a value
Confusing static and instance fields
5. You want to create a class BankAccount that remembers the balance for each account. Which design correctly uses instance fields to track the balance and safely update it?
class BankAccount {
private decimal balance;
public BankAccount(decimal initial) {
balance = initial;
}
public void Deposit(decimal amount) {
if (amount > 0) {
balance += amount;
}
}
public bool Withdraw(decimal amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
return true;
}
return false;
}
public decimal GetBalance() {
return balance;
}
}
hard
A. Incorrect: Deposit and Withdraw should be static methods
B. Incorrect: balance should be static to share across accounts
C. Correct design: instance field stores balance, methods update and read it safely
D. Incorrect: balance should be public to allow direct access
Solution
Step 1: Check if balance is instance field and encapsulated
Balance is private instance field, unique per object, which is correct for tracking each account.
Step 2: Verify methods safely update and provide access
Deposit and Withdraw check amounts before changing balance, and GetBalance returns current balance safely.
Final Answer:
Correct design: instance field stores balance, methods update and read it safely -> Option C
Quick Check:
Instance field + safe methods = correct state management [OK]
Hint: Use private instance fields with methods to control access [OK]
Common Mistakes:
Making balance static, sharing state wrongly
Using static methods that can't access instance fields