C# Variables MCQ

1. What is the default value of an int in C#?

a) 0
b) 1
c) null
d) undefined

Answer:

a) 0

Explanation:

In C#, the default value of an int (integer) type is 0.

2. Which of the following is a valid declaration of a variable in C#?

a) int 1x;
b) float myNumber;
c) double double;
d) bool false;

Answer:

b) float myNumber;

Explanation:

In C#, variable names cannot start with a number, be a keyword, or match the name of built-in data types.

3. How do you declare a constant variable in C#?

a) var CONSTANT_NAME;
b) const int CONSTANT_NAME;
c) static CONSTANT_NAME;
d) final CONSTANT_NAME;

Answer:

b) const int CONSTANT_NAME;

Explanation:

In C#, a constant variable is declared using the 'const' keyword followed by the data type and the variable name.

4. What is the correct way to declare a nullable integer in C#?

a) int? x = null;
b) nullable int x = null;
c) int x = null;
d) int x = Nullable<int>();

Answer:

a) int? x = null;

Explanation:

In C#, a nullable integer is declared using the int? syntax.

5. Which of the following data types has the largest storage capacity in C#?

a) int
b) long
c) short
d) byte

Answer:

b) long

Explanation:

In C#, the long data type has a larger storage capacity compared to int, short, and byte.

6. What does the following declaration represent in C#? string[] names;

a) An array of integers
b) An array of strings
c) A single string variable
d) A list of strings

Answer:

b) An array of strings

Explanation:

In C#, string[] is the syntax used for declaring an array of strings.

7. Which keyword is used to declare a variable that can be modified by different threads safely in C#?

a) static
b) volatile
c) mutable
d) readonly

Answer:

b) volatile

Explanation:

The 'volatile' keyword in C# is used to indicate that a field might be modified by multiple threads.

8. In C#, which of the following is the correct way to declare a variable for floating point numbers with high precision?

a) float num = 3.14;
b) double num = 3.14;
c) decimal num = 3.14m;
d) long num = 3.14;

Answer:

c) decimal num = 3.14m;

Explanation:

In C#, the 'decimal' type is used for high-precision floating point numbers. The 'm' suffix is used for decimal literals.

9. What is the output of the following C# code?

   int x = 10;
   int y = x++;
a) x=10, y=10
b) x=11, y=10
c) x=10, y=11
d) x=11, y=11

Answer:

b) x=11, y=10

Explanation:

The post-increment operator (x++) increments x after its current value is assigned to y. So, y gets 10 and x becomes 11.

10. Which of the following is true about the 'var' keyword in C#?

a) It declares a constant variable.
b) It is used for explicitly typed variables.
c) It allows the type of the variable to be determined at compile-time.
d) It indicates a variable with dynamic type.

Answer:

c) It allows the type of the variable to be determined at compile-time.

Explanation:

The 'var' keyword in C# enables implicit type declaration, allowing the compiler to infer the type of the variable at compile time.

11. In C#, which data type is used to store a single character?

a) String
b) Char
c) Byte
d) Short

Answer:

b) Char

Explanation:

The 'Char' data type in C# is used to store a single Unicode character.

12. What is the range of values for a ushort data type in C#?

a) -32,768 to 32,767
b) 0 to 65,535
c) -2,147,483,648 to 2,147,483,647
d) 0 to 4,294,967,295

Answer:

b) 0 to 65,535

Explanation:

The 'ushort' (unsigned short) data type in C# has a range of 0 to 65,535.

13. Which of the following correctly declares a multi-dimensional array in C#?

a) int[,] multiArray;
b) int[][] multiArray;
c) int[2][2] multiArray;
d) array[,] multiArray;

Answer:

a) int[,] multiArray;

Explanation:

In C#, a multi-dimensional array is declared using the syntax 'int[,]'.

14. What is the default value of a boolean variable in C#?

a) true
b) false
c) 0
d) null

Answer:

b) false

Explanation:

The default value of a boolean variable in C# is 'false'.

15. In C#, what is the output of Console.WriteLine(5 / 2);?

a) 2
b) 2.5
c) 3
d) 2.0

Answer:

a) 2

Explanation:

In C#, the division of two integers results in an integer. 5 / 2 yields 2 since the fractional part is discarded.

16. How do you explicitly convert a float to an int in C#?

a) int x = (int)floatVar;
b) int x = int.Parse(floatVar);
c) int x = int(floatVar);
d) int x = Convert.ToInt32(floatVar);

Answer:

a) int x = (int)floatVar;

Explanation:

Explicit casting is done using the (int) syntax in C#.

17. What is the purpose of the 'dynamic' keyword in C#?

a) To declare variables whose type can change at runtime.
b) To create anonymous types.
c) To define constants.
d) To declare static variables.

Answer:

a) To declare variables whose type can change at runtime.

Explanation:

The 'dynamic' keyword in C# is used for variables that are dynamically typed, allowing their type to be determined at runtime.

18. Which of the following is a valid way to declare a string in C#?

a) string s = "Hello, World!";
b) char s = "Hello, World!";
c) String s = "Hello, World!";
d) Both a and c

Answer:

d) Both a and c

Explanation:

In C#, both 'string' (alias) and 'String' (class name in the System namespace) are valid for string declarations.

19. What does the 'readonly' keyword do in C#?

a) Makes a field constant.
b) Prevents a field from being modified after construction.
c) Declares a static variable.
d) Indicates that a method does not return any value.

Answer:

b) Prevents a field from being modified after construction.

Explanation:

The 'readonly' keyword in C# indicates that assignment to the field can only occur as part of the declaration or in a constructor in the same class.

20. What is the maximum value of a 'byte' data type in C#?

a) 255
b) 127
c) 256
d) 128

Answer:

a) 255

Explanation:

In C#, the 'byte' data type is an 8-bit unsigned integer with a range from 0 to 255.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top