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.
varintstringdeclarenull0undefinedNaNstring name = "John";str name = "John";var name = "John";char name = 'John';float num = 10.5float num = 10.5fdouble num = 10.5ffloat num = 10.5dint[] arr = new int[5];int arr[5] = new int[];int arr = new int[5];new int[] arr = {1,2,3,4,5};boolbooleanbitbinarydecimal in C#?decimal price = 10.99f;decimal price = 10.99;float price = 10.99;decimal price = 10.99m;const int num = 5;static int num = 5;readonly int num = 5;int num = 5;int? num = null;int num? = null;nullable<int> num = null;num = null;char in C#?char letter = 'A';char letter = "A";letter = 'A';char letter = 'A';const and readonly in C#?const can be modified after initialization, readonly cannot.readonly can be initialized at runtime, const cannot.const can only be used with integers, readonly can be used with any type.readonly in C#?readonly variable?const string message = "Hello World";readonly string message = "Hello World";const message = "Hello World";string message = "Hello World";const variable at runtime?const variables are evaluated at runtime.const variables are evaluated at compile time.const is initialized within a method.staticreadonlyimmutableconstreadonly variable in a switch statement in C#?int.readonly variables cannot be used in switch statements.readonly variables can be used in any statement.readonly field after it has been initialized?readonly values after initialization.readonly fields can only be set once, at runtime.readonly variables can be modified anytime.myField)MyField)my_field)MYFIELD)calculateTotal())CalculateTotal())calculate_total())calculatetotal())MAX_VALUE)maxValue)MaxValue)MAXVALUE)totalAmount)TotalAmount)total_amount)TOTALAMOUNT)null as a valueint num = null;int? num = null;Nullable<int> num = null;int num = ?null;null0falseundefinedif (variable.HasValue)if (variable == null)if (variable != null)if (variable.IsValid).Value property of a nullable type in C# do?null| Qno | Answer |
|---|---|
| 1 | A) var |
| 2 | B) 0 |
| 3 | A) string name = "John"; |
| 4 | B) float num = 10.5f |
| 5 | A) int[] arr = new int[5]; |
| 6 | A) bool |
| 7 | D) decimal price = 10.99m; |
| 8 | A) const int num = 5; |
| 9 | A) int? num = null; |
| 10 | A) char letter = 'A'; |
| 11 | B) readonly |
| 12 | A) A static variable |
| 13 | B) Only in the constructor |
| 14 | A) const string message = "Hello World"; |
| 15 | C) Both value types and reference types |
| 16 | B) No, const variables are evaluated at compile time. |
| 17 | D) const |
| 18 | C) Yes, readonly variables can be used in any statement. |
| 19 | A) Constants can be initialized only once. |
| 20 | C) No, but you can assign a value at runtime through the constructor. |
| 21 | A) Camel case (e.g., myField) |
| 22 | B) Pascal case (e.g., CalculateTotal()) |
| 23 | A) Uppercase letters with underscores (e.g., MAX_VALUE) |
| 24 | A) Camel case (e.g., totalAmount) |
| 25 | C) Camel case |
| 26 | A) A type that can hold null as a value |
| 27 | B) int? num = null; |
| 28 | A) null |
| 29 | A) if (variable.HasValue) |
| 30 | B) Throws an exception if the nullable is null |