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

Constants and readonly fields 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 a constant integer named MaxValue with value 100.

C Sharp (C#)
public class Config {
    public const int MaxValue = [1];
}
Drag options to blanks, or click blank then click option'
Aint
Breadonly
CMaxValue
D100
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'readonly' instead of a value.
Not assigning a value to the constant.
2fill in blank
medium

Complete the code to declare a readonly field named creationTime initialized in the constructor.

C Sharp (C#)
public class Logger {
    public readonly DateTime creationTime;

    public Logger() {
        creationTime = [1];
    }
}
Drag options to blanks, or click blank then click option'
ADateTime
Breadonly
CDateTime.Now
Dnew DateTime()
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to assign a type name instead of a value.
Using 'readonly' keyword as a value.
3fill in blank
hard

Complete the code to initialize the readonly field named configName in the constructor.

C Sharp (C#)
public class Settings {
    public readonly string configName;

    public Settings(string name) {
        configName = [1];
    }
}
Drag options to blanks, or click blank then click option'
Aname
B"default"
Cthis.name
DconfigName
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string literal instead of the parameter.
Using 'this.name' which does not refer to the parameter.
4fill in blank
hard

Fill both blanks to declare a constant double Pi and a readonly double radius initialized in the constructor.

C Sharp (C#)
public class Circle {
    public const double Pi = [1];
    public readonly double radius;

    public Circle(double r) {
        radius = [2];
    }
}
Drag options to blanks, or click blank then click option'
A3.14159
Br
Cradius
D3.14
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning radius to itself instead of the parameter.
Using an incorrect value for Pi.
5fill in blank
hard

Fill all three blanks to declare a constant int MaxScore, a readonly int currentScore initialized in the constructor, and a method returning their sum.

C Sharp (C#)
public class Game {
    public const int MaxScore = [1];
    public readonly int currentScore;

    public Game(int score) {
        currentScore = [2];
    }

    public int TotalScore() {
        return MaxScore + [3];
    }
}
Drag options to blanks, or click blank then click option'
A100
Bscore
CcurrentScore
D50
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect variable names in the return statement.
Assigning readonly fields outside constructor.