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

Instance fields and state in C Sharp (C#) - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare an instance field named 'count' of type int.

C Sharp (C#)
public class Counter {
    private [1] count;
}
Drag options to blanks, or click blank then click option'
Aint
Bstatic
Cstring
Dvoid
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'void' as a type for a field.
Using 'static' keyword instead of a type.
Using 'string' when the field should hold numbers.
2fill in blank
medium

Complete the code to initialize the instance field 'count' to zero in the constructor.

C Sharp (C#)
public class Counter {
    private int count;
    public Counter() {
        [1] = 0;
    }
}
Drag options to blanks, or click blank then click option'
Athis
BCounter
Cvoid
Dcount
Attempts:
3 left
💡 Hint
Common Mistakes
Using the class name 'Counter' instead of the field name.
Using 'this' alone without the field name.
Using 'void' which is not a variable.
3fill in blank
hard

Fix the error in the method to correctly increment the instance field 'count'.

C Sharp (C#)
public class Counter {
    private int count;
    public void Increment() {
        [1]++;
    }
}
Drag options to blanks, or click blank then click option'
AIncrement
BCounter
Ccount
Dthis
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to increment the class name 'Counter'.
Using the method name 'Increment' as a variable.
Using 'this' alone without the field name.
4fill in blank
hard

Fill both blanks to create a method that returns the current value of the instance field 'count'.

C Sharp (C#)
public class Counter {
    private int count;
    public [1] GetCount() {
        return [2];
    }
}
Drag options to blanks, or click blank then click option'
Aint
Bvoid
Ccount
DCounter
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'void' as return type when the method returns a value.
Returning the class name instead of the field.
Returning nothing or wrong variable.
5fill in blank
hard

Fill all three blanks to define a class with an instance field, a constructor that sets it, and a method that returns it.

C Sharp (C#)
public class Person {
    private string [1];
    public Person(string [2]) {
        this.name = [2];
    }
    public string GetName() {
        return [3];
    }
}
Drag options to blanks, or click blank then click option'
Aname
Dperson
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for the field and parameter causing confusion.
Not using 'this' to distinguish the field from the parameter.
Returning a variable that does not exist.