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

Constants and readonly fields in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Constant Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of constant and readonly fields
What is the output of this C# program?
C Sharp (C#)
using System;
class Test {
    public const int ConstValue = 10;
    public readonly int ReadonlyValue;
    public Test(int val) {
        ReadonlyValue = val;
    }
    public void PrintValues() {
        Console.WriteLine($"ConstValue = {ConstValue}, ReadonlyValue = {ReadonlyValue}");
    }
}
class Program {
    static void Main() {
        Test t = new Test(20);
        t.PrintValues();
    }
}
AConstValue = 20, ReadonlyValue = 10
BConstValue = 10, ReadonlyValue = 10
CConstValue = 0, ReadonlyValue = 20
DConstValue = 10, ReadonlyValue = 20
Attempts:
2 left
💡 Hint
Remember that const values are fixed at compile time and readonly values can be set in the constructor.
Predict Output
intermediate
2:00remaining
Readonly field modification attempt
What happens when you try to compile and run this code?
C Sharp (C#)
class Sample {
    public readonly int ReadonlyField = 5;
    public void Change() {
        ReadonlyField = 10;
    }
}
class Program {
    static void Main() {
        Sample s = new Sample();
        s.Change();
        System.Console.WriteLine(s.ReadonlyField);
    }
}
ACompilation error: Cannot assign to 'ReadonlyField' because it is readonly
BOutput: 10
COutput: 5
DRuntime error: Field is readonly
Attempts:
2 left
💡 Hint
Readonly fields can only be assigned in declaration or constructor.
🧠 Conceptual
advanced
2:00remaining
Difference between const and readonly
Which statement correctly describes the difference between const and readonly fields in C#?
Areadonly values are set at compile time and cannot change; const values can be set at runtime but only once.
Bconst values are set at compile time and cannot change; readonly values can be set at runtime but only once.
CBoth const and readonly values can be changed anytime during program execution.
Dconst and readonly are interchangeable and have no difference.
Attempts:
2 left
💡 Hint
Think about when the values are assigned and if they can change after that.
Predict Output
advanced
2:00remaining
Readonly field with static constructor
What is the output of this program?
C Sharp (C#)
using System;
class Example {
    public static readonly int Value;
    static Example() {
        Value = 42;
    }
}
class Program {
    static void Main() {
        Console.WriteLine(Example.Value);
    }
}
A42
B0
CCompilation error: readonly field must be assigned inline or in instance constructor
DRuntime error: static constructor failed
Attempts:
2 left
💡 Hint
Static readonly fields can be assigned in static constructors.
Predict Output
expert
2:00remaining
Constant expression evaluation
What is the output of this program?
C Sharp (C#)
using System;
class ConstTest {
    public const int A = 5;
    public const int B = A * 2;
    public const int C = B + 3;
}
class Program {
    static void Main() {
        Console.WriteLine(ConstTest.C);
    }
}
A8
B10
C13
DCompilation error: const expressions cannot use other const fields
Attempts:
2 left
💡 Hint
Const expressions can use other const fields in their definitions.