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

Methods that operate on 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 define a method that changes the value of the field.

C Sharp (C#)
class Counter {
    private int count = 0;
    public void [1]() {
        count++;
    }
}
Drag options to blanks, or click blank then click option'
AIncrement
BGetCount
CReset
DDisplay
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method name that does not describe changing the state, like GetCount.
2fill in blank
medium

Complete the method to reset the count field to zero.

C Sharp (C#)
class Counter {
    private int count = 0;
    public void Reset() {
        count [1] 0;
    }
}
Drag options to blanks, or click blank then click option'
A=
B==
C+=
D++
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '=' causes a syntax error.
Using '+=' or '++' changes the value incorrectly.
3fill in blank
hard

Fix the error in the method that returns the current count.

C Sharp (C#)
class Counter {
    private int count = 0;
    public int GetCount() {
        return [1];
    }
}
Drag options to blanks, or click blank then click option'
ACount
Bthis.count()
CgetCount
Dcount
Attempts:
3 left
💡 Hint
Common Mistakes
Using uppercase 'Count' which is undefined.
Trying to call count as a method with parentheses.
4fill in blank
hard

Fill both blanks to create a method that adds a given number to the count.

C Sharp (C#)
class Counter {
    private int count = 0;
    public void Add([1] number) {
        count [2] number;
    }
}
Drag options to blanks, or click blank then click option'
Aint
B+=
C-=
Dfloat
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' operator instead of '+='.
Using a float parameter when count is int.
5fill in blank
hard

Fill all three blanks to create a method that multiplies count by a factor and returns the result.

C Sharp (C#)
class Counter {
    private int count = 1;
    public int MultiplyBy([1] factor) {
        count [2] factor;
        return [3];
    }
}
Drag options to blanks, or click blank then click option'
Aint
B*=
Ccount
Ddouble
Attempts:
3 left
💡 Hint
Common Mistakes
Returning the factor instead of count.
Using double type when count is int.