Constant Variables Flashcards
What is a constant variable in C#?
A constant variable is a variable whose value cannot be changed after it is initialized.
True or False: Constant variables in C# can be assigned a new value after they are declared.
False
Fill in the blank: In C#, a constant variable is declared using the keyword ____.
const
What is the scope of a constant variable in C#?
The scope of a constant variable is determined by where it is declared; it can be local, class-level, or global.
Which of the following data types can be used for constant variables in C#? (A) int (B) string (C) double (D) All of the above
D) All of the above
What is the difference between const and readonly in C#?
A const variable must be assigned at compile time, while a readonly variable can be assigned at runtime but only in the constructor.
True or False: Constant variables in C# can be initialized with values that are computed at runtime.
False
What is the default access modifier for constant variables declared at the class level in C#?
The default access modifier is ‘private’.
Can a constant variable be of a reference type in C#?
Yes, a constant variable can be of a reference type, but it cannot change the reference it points to.
What happens if you try to change the value of a constant variable in C#?
The compiler will throw an error.
Fill in the blank: The value of a constant variable must be a ____ value.
literal
Which of the following is a valid declaration of a constant variable in C#? (A) const int x = 10; (B) const int x; x = 10; (C) int const x = 10; (D) const int x = 10, y = 20;
A) const int x = 10;
True or False: Constant variables can be used in switch statements in C#.
True
What is the purpose of using constant variables in C#?
To define values that are not meant to change, improving code readability and maintainability.
Fill in the blank: Constant variables can be declared with ____ values.
static
Which keyword is used to define a variable that can only be assigned once and can be set at runtime?
readonly
True or False: You can declare a constant variable inside a method in C#.
True
What is the effect of using constant variables in loop conditions?
Constant variables can improve performance by avoiding repeated evaluations of the same value.
Fill in the blank: You cannot use a constant variable to initialize a ____ variable.
non-constant
Can constant variables be used in attribute arguments in C#?
Yes, they can be used in attribute arguments.
What is the naming convention for constant variables in C#?
Constant variables are typically named using all uppercase letters with underscores separating words.
True or False: Constant variables are stored in the stack memory.
True
What happens if a constant variable is not initialized at the time of declaration?
It will cause a compile-time error.
Fill in the blank: You can use ____ to define multiple constant variables in one line.
commas