MCQs on Variables and Constants | C#

Dive deep into C# variables, constants, and nullable types with these 30 multiple-choice questions. Learn how to declare variables, use constants, follow naming conventions, and work with nullable types.


Topic 1: Declaring Variables (10 Questions)

  1. Which keyword is used to declare a variable in C#?
    A) var
    B) int
    C) string
    D) declare
  2. What is the default value of an integer variable in C#?
    A) null
    B) 0
    C) undefined
    D) NaN
  3. How do you declare a string variable in C#?
    A) string name = "John";
    B) str name = "John";
    C) var name = "John";
    D) char name = 'John';
  4. Which of the following is a valid declaration of a float variable in C#?
    A) float num = 10.5
    B) float num = 10.5f
    C) double num = 10.5f
    D) float num = 10.5d
  5. What is the correct syntax to declare an array of integers in C#?
    A) int[] arr = new int[5];
    B) int arr[5] = new int[];
    C) int arr = new int[5];
    D) new int[] arr = {1,2,3,4,5};
  6. Which type is used to declare a variable that holds a boolean value in C#?
    A) bool
    B) boolean
    C) bit
    D) binary
  7. How do you declare a variable of type decimal in C#?
    A) decimal price = 10.99f;
    B) decimal price = 10.99;
    C) float price = 10.99;
    D) decimal price = 10.99m;
  8. What is the correct declaration for a constant in C#?
    A) const int num = 5;
    B) static int num = 5;
    C) readonly int num = 5;
    D) int num = 5;
  9. How do you declare a variable with a nullable integer in C#?
    A) int? num = null;
    B) int num? = null;
    C) nullable<int> num = null;
    D) num = null;
  10. Which of the following is a correct way to declare a variable of type char in C#?
    A) char letter = 'A';
    B) char letter = "A";
    C) letter = 'A';
    D) char letter = 'A';

Topic 2: Constants and Readonly (10 Questions)

  1. What is the difference between const and readonly in C#?
    A) const can be modified after initialization, readonly cannot.
    B) readonly can be initialized at runtime, const cannot.
    C) const can only be used with integers, readonly can be used with any type.
    D) There is no difference.
  2. Which of the following can be declared as readonly in C#?
    A) A static variable
    B) A method
    C) A constructor
    D) A constant value
  3. When can you assign a value to a readonly variable?
    A) At runtime
    B) Only in the constructor
    C) After the class is initialized
    D) At compile time
  4. Which of the following is a correct declaration of a constant string in C#?
    A) const string message = "Hello World";
    B) readonly string message = "Hello World";
    C) const message = "Hello World";
    D) string message = "Hello World";
  5. What type of data can be assigned to a constant in C#?
    A) Only primitive types
    B) Only value types
    C) Both value types and reference types
    D) Only reference types
  6. Can you assign a value to a const variable at runtime?
    A) Yes, const variables are evaluated at runtime.
    B) No, const variables are evaluated at compile time.
    C) Only for static variables.
    D) Only if the const is initialized within a method.
  7. Which of the following keywords is used to declare a constant in C#?
    A) static
    B) readonly
    C) immutable
    D) const
  8. Can you use a readonly variable in a switch statement in C#?
    A) Yes, if the variable is of type int.
    B) No, readonly variables cannot be used in switch statements.
    C) Yes, readonly variables can be used in any statement.
    D) Yes, but only in a constant expression.
  9. Which of the following is true about constants in C#?
    A) Constants can be initialized only once.
    B) Constants can be initialized at runtime.
    C) Constants can be changed anytime in the program.
    D) Constants can be only of numeric types.
  10. Can you modify the value of a readonly field after it has been initialized?
    A) Yes, you can modify readonly values after initialization.
    B) No, readonly fields can only be set once, at runtime.
    C) No, but you can assign a value at runtime through the constructor.
    D) Yes, readonly variables can be modified anytime.

Topic 3: Naming Conventions (5 Questions)

  1. What is the recommended naming convention for private fields in C#?
    A) Camel case (e.g., myField)
    B) Pascal case (e.g., MyField)
    C) Snake case (e.g., my_field)
    D) Uppercase (e.g., MYFIELD)
  2. Which of the following is a correct naming convention for method names in C#?
    A) Camel case (e.g., calculateTotal())
    B) Pascal case (e.g., CalculateTotal())
    C) Snake case (e.g., calculate_total())
    D) Lowercase (e.g., calculatetotal())
  3. How should constants be named in C#?
    A) Uppercase letters with underscores (e.g., MAX_VALUE)
    B) Camel case with no underscores (e.g., maxValue)
    C) Pascal case (e.g., MaxValue)
    D) Uppercase letters without underscores (e.g., MAXVALUE)
  4. In C#, what is the preferred style for naming local variables?
    A) Camel case (e.g., totalAmount)
    B) Pascal case (e.g., TotalAmount)
    C) Snake case (e.g., total_amount)
    D) Uppercase letters (e.g., TOTALAMOUNT)
  5. Which naming convention is used for method parameters in C#?
    A) Pascal case
    B) Snake case
    C) Camel case
    D) Uppercase

Topic 4: Nullable Types (5 Questions)

  1. What is a nullable type in C#?
    A) A type that can hold null as a value
    B) A type that can hold any value
    C) A type that cannot hold a value
    D) A type that can only hold numbers
  2. How do you declare a nullable integer variable in C#?
    A) int num = null;
    B) int? num = null;
    C) Nullable<int> num = null;
    D) int num = ?null;
  3. Which of the following is the default value for a nullable type in C#?
    A) null
    B) 0
    C) false
    D) undefined
  4. How do you check if a nullable type has a value in C#?
    A) if (variable.HasValue)
    B) if (variable == null)
    C) if (variable != null)
    D) if (variable.IsValid)
  5. What does the .Value property of a nullable type in C# do?
    A) Returns the default value if the nullable is null
    B) Throws an exception if the nullable is null
    C) Returns the current value or null
    D) Converts the nullable type to a non-nullable type

Answer Key

QnoAnswer
1A) var
2B) 0
3A) string name = "John";
4B) float num = 10.5f
5A) int[] arr = new int[5];
6A) bool
7D) decimal price = 10.99m;
8A) const int num = 5;
9A) int? num = null;
10A) char letter = 'A';
11B) readonly
12A) A static variable
13B) Only in the constructor
14A) const string message = "Hello World";
15C) Both value types and reference types
16B) No, const variables are evaluated at compile time.
17D) const
18C) Yes, readonly variables can be used in any statement.
19A) Constants can be initialized only once.
20C) No, but you can assign a value at runtime through the constructor.
21A) Camel case (e.g., myField)
22B) Pascal case (e.g., CalculateTotal())
23A) Uppercase letters with underscores (e.g., MAX_VALUE)
24A) Camel case (e.g., totalAmount)
25C) Camel case
26A) A type that can hold null as a value
27B) int? num = null;
28A) null
29A) if (variable.HasValue)
30B) Throws an exception if the nullable is null

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top