Constant Variables Flashcards

1
Q

What is a constant variable in C#?

A

A constant variable is a variable whose value cannot be changed after it is initialized.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

True or False: Constant variables in C# can be assigned a new value after they are declared.

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Fill in the blank: In C#, a constant variable is declared using the keyword ____.

A

const

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the scope of a constant variable in C#?

A

The scope of a constant variable is determined by where it is declared; it can be local, class-level, or global.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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

A

D) All of the above

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the difference between const and readonly in C#?

A

A const variable must be assigned at compile time, while a readonly variable can be assigned at runtime but only in the constructor.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

True or False: Constant variables in C# can be initialized with values that are computed at runtime.

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the default access modifier for constant variables declared at the class level in C#?

A

The default access modifier is ‘private’.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Can a constant variable be of a reference type in C#?

A

Yes, a constant variable can be of a reference type, but it cannot change the reference it points to.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What happens if you try to change the value of a constant variable in C#?

A

The compiler will throw an error.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Fill in the blank: The value of a constant variable must be a ____ value.

A

literal

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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

A) const int x = 10;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

True or False: Constant variables can be used in switch statements in C#.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the purpose of using constant variables in C#?

A

To define values that are not meant to change, improving code readability and maintainability.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Fill in the blank: Constant variables can be declared with ____ values.

A

static

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Which keyword is used to define a variable that can only be assigned once and can be set at runtime?

17
Q

True or False: You can declare a constant variable inside a method in C#.

18
Q

What is the effect of using constant variables in loop conditions?

A

Constant variables can improve performance by avoiding repeated evaluations of the same value.

19
Q

Fill in the blank: You cannot use a constant variable to initialize a ____ variable.

A

non-constant

20
Q

Can constant variables be used in attribute arguments in C#?

A

Yes, they can be used in attribute arguments.

21
Q

What is the naming convention for constant variables in C#?

A

Constant variables are typically named using all uppercase letters with underscores separating words.

22
Q

True or False: Constant variables are stored in the stack memory.

23
Q

What happens if a constant variable is not initialized at the time of declaration?

A

It will cause a compile-time error.

24
Q

Fill in the blank: You can use ____ to define multiple constant variables in one line.