C# MCQ 2024
C# MCQ 2024
a) using CSharp;
b) using System.Collections;
c) using System;
d) using Base;
2. What is the Console.WriteLine() function do?
a) Reads input
b) Writes to a file
c) Writes to the console with a new line
d) Clears the console
3. Which keyword is used to create an instance of a class?
a) class
b) new
c) struct
d) this
4. Which of the following data types can store a non-integer number?
a) int
b) char
c) double
d) bool
5. What does the static keyword mean in C#?
a) The variable can change
b) The method can be overridden
c) The member belongs to the type rather than any specific instance
d) The value can be assigned only once
6. What is the purpose of the Main method in C#?
a) To define variables
b) To serve as the entry point for the application
c) To close the application
d) To initialize objects
7. Which of the following is NOT a valid access modifier in C#?
a) private
b) public
c) transient
d) protected
8. What is encapsulation?
a) Breaking the code into small functions
b) Wrapping up data members and methods into a single unit
c) Inheriting properties of one class into another
d) Implementing multiple interfaces
9. Which keyword is used to inherit from a base class in C#?
a) extends
b) inherits
c) implement
d) :
10. Which type of exception handling uses a try-catch block?
a) Unchecked
b) Implicit
c) Explicit
d) Checked
11. Which of the following is NOT a loop structure in C#?
a) for
b) while
c) loop
d) foreach
12. Which operator is used for type casting in C#?
a) ::
b) as
c) cast
d) ->
13. Which of the following is the correct way to comment out multiple lines in C#?
a) // …
b) /* … */
c) — … —
14. Which keyword is used to declare a constant variable in C#?
a) constant
b) con
c) const
d) final
15. Which of the following data types can store a Unicode character in C#?
a) char
b) string
c) byte
d) short
16. How do you define a method in C# that does not return a value?
a) void
b) null
c) empty
d) returnless
17. Which of the following statements about C# interfaces is TRUE?
a) They can contain the implementation of methods.
b) They can be instantiated.
c) They can have fields.
d) They can declare methods without implementations.
18. Which of the following access modifiers allows a member to be accessed from any
class within the same assembly but not from outside the assembly?
a) private
b) public
c) protected
d) internal
19. Which of the following is NOT a type of constructor in C#?
a) Copy constructor
b) Static constructor
c) Default constructor
d) Derived constructor
20. What does the ?? operator do in C#?
a) Compares two values
b) Assign one of two values depending on a condition
c) Checks for nullability and returns the non-null value
d) Merges two values
21. What is boxing in C#?
a) Placing a value type inside an object
b) Extracting a value type from an object
c) Creating an array of objects
d) Defining multiple boxes for UI design
22. What does the override keyword do in C#?
a) Hides the base class method
b) Makes a method static
c) Provides a new implementation for a virtual method in a derived class
d) Makes a method abstract
23. Which collection in C# does NOT allow duplicate elements?
a) List
b) Array
c) Hashtable
d) HashSet
24. How do you declare a single-dimensional array in C#?
a) int x[];
b) int x();
c) int[] x;
d) int{} x;
25. What is LINQ in C#?
a) A collection of functions
b) A new programming language
c) A library for parallel programming
d) A framework for querying data in a consistent manner
26. Which of the following statements correctly initializes a string to an empty value?
a) string str = null;
b) string str = “”;
c) string str = ” “;
d) string str = String.Empty; //more readable way of refresenting an empty string
27. What does the finally block in exception handling do?
a) Catches exceptions
b) Throws exceptions
c) Executes regardless of whether an exception is thrown or caught
d) Stops exceptions from propagating
28. Which of the following keywords is used to declare a constant variable?
a) const
b) readonly
c) final
d) static
29. Which of the following methods is used to determine the length of a string in C#?
a) Length()
b) Size()
c) Count()
d) Length
30. What is the base class for all classes in C#?
a) Object
b) Base
c) Root
d) System
31. Which of the following is NOT a valid access modifier in C#?
a) public
b) private
c) protected
d) secured
32. Which of the following types is a reference type?
a) int
b) char
c) bool
d) string
33. In C#, what does the is keyword do?
a) Checks type compatibility
b) Performs a deep copy of objects
c) Implements inheritance
d) Compares two strings
34. How can you make a class in C# so that it cannot be inherited?
a) final
b) static
c) sealed
d) abstract
35. Which of the following is the correct way to define an anonymous method in C#?
a) delegate void() { }
b) func void() { }
c) delegate() { }
d) delegate void { }
36. Which of the following correctly declares a nullable int in C#?
a) int? x;
b) nullable int x;
c) int x?;
d) int x = null;
37. Which of the following is NOT a valid C# collection?
a) ArrayList
b) HashTable
c) LinkList
d) Queue
38. Which method is called to free the resources of an object manually in C#?
a) delete()
b) free()
c) remove()
d) Dispose()
39. Which of the following keywords is used to handle an exception in C#?
a) catch
b) throw
c) error
d) handle
40. What does the as keyword do in C#?
a) It performs a deep copy of objects
b) It checks for type compatibility and returns null if the conversion is not
possible
c) It compares two strings
d) It enforces inheritance
41. Which of the following types can be used to declare a variable that can range from -
2,147,483,648 to 2,147,483,647?
a) long
b) short
c) int
d) byte
42. Which of the following keywords is used to declare an abstract class in C#?
a) abstracted
b) abs
c) abstract
d) abstraction
43. Which of the following methods can be overridden in a derived class?
a) Static method
b) Virtual method
c) Sealed method
d) Private method
44. What is the default value of a bool in C#?
a) true
b) false
c) null
d) 0
45. Which collection in C# ensures that all elements are unique?
a) List
b) Dictionary
c) Queue
d) HashSet
46. Which operator can be used to determine the type of an object at runtime?
a) is
b) typeof
c) sizeof
d) type
47. Which of the following statements about delegates in C# is true?
a) Delegates can point to instance methods only.
b) Delegates cannot be used with static methods.
c) Delegates are type-safe.
d) Delegates can’t be used with lambda expressions.
48. Which of the following provides a way to have multiple implementations for a
method?
a) Overloading
b) Overriding
c) Hiding
d) Abstracting
49. What does the virtual keyword indicate?
a) The method cannot be overridden in the derived class.
b) The method must be overridden in the derived class.
c) The method can be overridden in the derived class.
d) The method is abstract.
50. Which of the following statements correctly creates an array of 5 integers?
a) int[] array = new int[5];
b) int[] array = 5;
c) int array[5];
d) int array = new int[5];
51. Which of the following is correct about C#?
a) It is component oriented.
b) It can be compiled on a variety of computer platforms.
c) It is a part of .Net Framework.
d) All of the above.
52. Which of the following converts a type to a byte value in C#?
a) ToSingle
b) ToByte
c) ToChar
d) ToDateTime
53. Which of the following converts a type (integer or string type) to date-time
structures in C#?
a) ToString
b) ToSingle
c) ToChar
d) ToDateTime
54. Which of the following operator casts without raising an exception if the cast fails in
C#?
a) ?:
b) is
c) as
d) *
55. Which of the following access specifier in C# allows a class to hide its member
variables and member functions from other class objects and functions, except a child
class within the same application?
a) Protected Internal
b) Private
c) Protected
d) Internal
56. Which of the following is correct about params in C#?
a) By using the params keyword, a method parameter can be specified which takes a
variable number of arguments or even no argument.
b) Additional parameters are not permitted after the params keyword in a method
declaration.
c) Only one params keyword is allowed in a method declaration.
d) All of the above.
C# Variables MCQ
1. What is the default value of an int in C#?
a) 0
b) 1
c) null
d) undefined
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;
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;
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>();
5. Which of the following data types has the largest storage capacity in C#?
a) int
b) long
c) short
d) 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
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
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;
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
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.
11. In C#, which data type is used to store a single character?
a) String
b) Char
c) Byte
d) Short
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
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;
14. What is the default value of a boolean variable in C#?
a) true
b) false
c) 0
d) null
15. In C#, what is the output of Console.WriteLine(5 / 2);?
a) 2
b) 2.5
c) 3
d) 2.0
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);
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.
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
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.
20. What is the maximum value of a 'byte' data type in C#?
a) 255
b) 127
c) 256
d) 128
C# Strings MCQ
1. How do you concatenate strings in C#?
a) Using the '+' operator
b) Using the '&' operator
c) Using the concat() method
d) Using the append() method
2. What method is used to compare two strings for equality in C#?
a) equals()
b) isEqual()
c) CompareTo()
d) Compare()
3. Which method in the String class returns the length of a string in C#?
a) Length
b) Count
c) Size
d) GetLength
4. How can you convert a string to uppercase in C#?
a) toUpperCase()
b) ToUpper()
c) Uppercase()
d) MakeUpper()
5. What is the output of the following code in C#?
a) Hello,
b) World!
c) ello, World!
d) , World!
6. What is the character used to denote a verbatim string in C#?
a) @
b) #
c) $
d) %
7. How do you split a string on specific characters in C#?
a) Using the Split() method
b) Using the Divide() method
c) Using the Break() method
d) Using the Cut() method
8. What does the Trim() method do in C#?
a) Removes all whitespace
b) Removes whitespace from the start and end of a string
c) Trims a string to a specified length
d) Removes specified characters from a string
9. How do you determine if a string starts with a specific substring in C#?
a) StartsWith()
b) BeginsWith()
c) HasPrefix()
d) LeadingString()
10. What is the result of string concatenation using the following code in C#?
a) HelloWorld
b) Hello World
c) Hello+World
d) Hello_World
11. How do you find the position of a substring within a string in C#?
a) Index()
b) FindIndex()
c) Locate()
d) IndexOf()
12. Which method is used to replace a specified substring with another substring in a
string in C#?
a) Change()
b) Switch()
c) Replace()
d) Modify()
13. What is the output of the following code in C#?
a) S
b) h
c) a
d) C
14. How do you convert a string to a number in C#?
a) int.Parse()
b) Convert.ToInt32()
c) StringToNumber()
d) Both a and b
15. What does the IsNullOrEmpty method do in C#?
a) Checks if a string is empty or consists of only whitespace characters
b) Checks if a string is null or has a length of zero
c) Checks if a string is null or consists of only whitespace characters
d) Checks if a string is null or empty array
16. How do you format a string in C# using placeholders?
a) Using the Format() method
b) Using string interpolation
c) Using the Concat() method
d) Both a and b
17. What is the escape sequence used for a new line in C#?
a) \\n
b) \n
c) /n
d) //n
18. How can you check if a string contains a specific substring in C#?
a) Contains()
b) Has()
c) Includes()
d) Find()
C# Arrays MCQ
1. How do you declare an array of integers in C#?
a) int[] myArray;
b) array<int> myArray;
c) int myArray[];
d) int array[] myArray;
2. What will be the default value of elements in a newly created int array in C#?
a) 0
b) 1
c) null
d) -1
3. How can you determine the length of an array in C#?
a) array.length
b) array.size
c) array.Length
d) array.count
4. Which of these is a correct way to initialize an array in C#?
a) int[] array = new int[5]{1, 2, 3, 4, 5};
b) int[] array = new int[]{1, 2, 3, 4, 5};
c) int array[] = {1, 2, 3, 4, 5};
d) int[] array = new int[5](1, 2, 3, 4, 5);
5. How do you access the third element in an array named 'data' in C#?
a) data[2];
b) data[3];
c) data(2);
d) data(3);
6. What is the output of the following C# code?
int[] nums = {1, 2, 3};
Console.WriteLine(nums[1]);
a) 1
b) 2
c) 3
d) Error
7. How do you create a two-dimensional array in C#?
a) int[,] multiArray = new int[2,3];
b) int[][] multiArray = new int[2][3];
c) int[2][3] multiArray = new int[][];
d) int[,] multiArray = { {1, 2}, {3, 4, 5} };
8. What does the following code do in C#?
Array.Sort(myArray);
a) Reverses the array
b) Sorts the array in descending order
c) Sorts the array in ascending order
d) Shuffles the array randomly
9. Which method is used to reverse the elements of an array in C#?
a) Array.Reverse()
b) Array.Flip()
c) Array.Invert()
d) Reverse.Array()
10. In C#, what exception is thrown if you access an array index that is out of bounds?
a) ArrayIndexOutOfBoundsException
b) IndexOutOfRangeException
c) ArgumentOutOfRangeException
d) InvalidOperationException
11. What is the correct way to declare a jagged array in C#?
a) int[][] jaggedArray;
b) int[,] jaggedArray;
c) int[] jaggedArray[];
d) int[][,] jaggedArray;
Click to View Answer and Explanation
12. How do you copy an array in C#?
a) Array.Copy(sourceArray, destinationArray, length);
b) sourceArray.Copy(destinationArray, length);
c) destinationArray = sourceArray.Copy(length);
d) destinationArray = sourceArray;
13. Which keyword is used to iterate over an array in C#?
a) foreach
b) for
c) in
d) iterate
14. What is the default value for elements in a boolean array in C#?
a) true
b) false
c) null
d) 0
15. How do you initialize an array of strings with 3 elements in C#?
a) string[] names = new string[3]{"John", "Jane", "Doe"};
b) string[] names = new string[]{"John", "Jane", "Doe"};
c) string[] names = {"John", "Jane", "Doe"};
d) All of the above
16. How do you check if an array is empty in C#?
a) array.IsEmpty()
b) array.Length == 0
c) array.Count() == 0
d) array == null
17. Which method is used to find a specific element in an array in C#?
a) Array.Find()
b) Array.Search()
c) Array.Get()
d) Array.Lookup()
18. What is the result of using the Clone() method on an array in C#?
a) It creates a deep copy of the array.
b) It creates a shallow copy of the array.
c) It sorts the array.
d) It reverses the array.
19. How do you declare an array with initial size but without initializing the elements in
C#?
a) int[] nums = new int[5];
b) int[] nums = new int[]{5};
c) int[] nums = new int[] {0, 0, 0, 0, 0};
d) int[] nums = {5};
20. Which of the following is not a valid array declaration in C#?
a) int[] arr = new int[5];
b) var arr = new int[5];
c) int[] arr = new int[]{1, 2, 3, 4, 5};
d) int arr[] = new int[5];
C# If … Else MCQ
1. Which of the following is the correct syntax for an if statement in C#?
a) if (condition) { /* code */ }
b) if condition { /* code */ }
c) if (condition) then { /* code */ }
d) if condition then { /* code */ }
2. How do you add an else statement in C#?
a) if (condition) { /* code if true */ } else { /* code if false */ }
b) if (condition) { /* code if true */ } otherwise { /* code if false */ }
c) if (condition) then { /* code if true */ } else { /* code if false */ }
d) if condition { /* code if true */ } else { /* code if false */ }
3. What is the purpose of the else if statement in C#?
a) To execute code if the previous if condition is true
b) To execute code if the previous if condition is false, and a new condition is true
c) To execute code regardless of the previous if condition
d) None of the above
4. Can the else statement in C# exist without the preceding if statement?
a) Yes, else can exist on its own
b) No, else must always follow an if
c) Yes, but only if there are no conditions
d) No, but it can follow an else if
5. How many else if statements can you have in a single if statement block in C#?
a) Only one
b) Up to five
c) As many as needed
d) None, else if is not allowed in C#
6. What is the result of using nested if statements in C#?
a) Increases code complexity and readability
b) Allows the checking of another condition within an if or else if block
c) Causes a syntax error
d) Reduces the efficiency of the code
7. Which of the following demonstrates the use of multiple conditions in an if statement
in C#?
a) if (condition1 && condition2) { /* code */ }
b) if condition1 and condition2 { /* code */ }
c) if (condition1 + condition2) { /* code */ }
d) if condition1 & condition2 { /* code */ }
8. What is the correct way to write an if statement without a code block in C#?
a) if (condition) statement;
b) if (condition) -> statement;
c) if (condition) : statement;
d) if condition -> statement;
9. How does C# handle the evaluation of conditions in an if-else if-else block?
a) All conditions are evaluated regardless of their outcome
b) Evaluates conditions until one is true, then skips the remaining
c) Evaluates only the first and last conditions
d) Evaluates all if conditions but skips the else
10. What will happen if the condition in an if statement is always true?
a) The program will enter an infinite loop
b) The if block will be ignored
c) The program will throw a runtime error
d) The if block will execute every time
11. In C#, what type of values can be used as conditions in an if statement?
a) Only boolean values
b) Integers
c) Any type, as long as it's not null
d) Boolean values and expressions that result in a boolean
12. Which of the following is not a valid way to write an if statement in C#?
a) if (1 == 1) { /* code */ }
b) if (true) { /* code */ }
c) if (false) { /* code */ } else { /* code */ }
d) if (x = 5) { /* code */ }
13. What happens if an if condition in C# evaluates to false and there is no else block?
a) The program throws an error
b) The if block executes with a default value
c) The program exits
d) The if block is skipped
14. Is it possible to have an if statement without an else statement in C#?
a) Yes, it is possible
b) No, else is mandatory
c) Only in certain conditions
d) Yes, but only inside a loop
15. What does the following code do in C#?
if (condition)
{
/* code block 1 */
}
else if (anotherCondition)
{
/* code block 2 */
}
else
{
/* code block 3 */
}
a) Executes code block 1 if condition is true, otherwise checks anotherCondition
b) Always executes code block 3
c) Executes each code block in order
d) Executes code block 1 and then code block 3
C# Switch MCQ
1. Which of the following is the correct syntax for a switch statement in C#?
a) switch(variable) { case value: /* code */ }
b) switch variable { case value: /* code */ }
c) switch(variable) { when value: /* code */ }
d) switch(variable) { if value: /* code */ }
2. What keyword is used to terminate a case in a switch statement in C#?
a) end
b) stop
c) break
d) exit
3. How does a switch statement in C# behave if the break statement is omitted in a case?
a) It will throw a compile-time error.
b) It will continue executing the next case.
c) It will exit the switch statement.
d) It will repeat the current case.
4. Which of the following data types can be used in a C# switch statement?
a) int
b) string
c) char
d) All of the above
5. What is the role of the 'default' case in a switch statement in C#?
a) To define the initial case
b) To catch any values not matched by other cases
c) To set a default return value
d) To optimize the switch performance
6. Can a case in a switch statement in C# have multiple values?
a) Yes, by using commas to separate values
b) No, each case can have only one value
c) Yes, by using the 'or' operator
d) No, it throws a syntax error
7. What is the output of the following code?
int x = 2;
switch (x)
{
case 1:
case 2:
Console.WriteLine("One or Two");
break;
default:
Console.WriteLine("Other");
}
a) One or Two
b) Other
c) No output
d) Error
8. How do you handle multiple conditions with the same code in a switch statement?
a) By using if-else statements inside the switch
b) By stacking case statements without a break until the last one
c) By creating a function and calling it in each case
d) By using the '&&' operator in the case statement
9. Which of the following is a valid use of the switch statement in C#?
a) switch(x) { case 1: /* code */; case 2: /* code */; default: /* code */ }
b) switch(x) { 1: /* code */; 2: /* code */; else: /* code */ }
c) switch(x) { when 1: /* code */; when 2: /* code */; otherwise: /* code */ }
d) switch(x) { if 1: /* code */; if 2: /* code */; else: /* code */ }
10. In C#, can a switch statement be nested inside another switch statement?
a) Yes
b) No
c) Only inside a default case
d) Only with a special compiler directive
11. What is required to use a string in a switch statement in C#?
a) The string must be constant.
b) The string should be converted to a char array.
c) No special requirements; strings can be used directly.
d) The string needs to be normalized.
12. What happens if the switch expression matches no case and there is no default case?
a) The first case is executed
b) The last case is executed
c) An exception is thrown
d) Nothing is executed
13. Can a switch statement in C# be used with boolean values?
a) Yes, but only with true and false cases
b) No, switch statements don't support boolean values
c) Yes, with any boolean expression
d) No, it requires conversion to an integer
14. How do you ensure that a switch case in C# falls through to the next case?
a) By using the fallthrough keyword
b) By omitting the break statement
c) By writing 'continue;'
d) C# does not allow fall-through by default
15. Which of the following is not a valid type for switch expression in C#?
a) int
b) enum
c) float
d) char
C# While Loop MCQ
1. What is the basic structure of a while loop in C#?
a) while (condition) { /* code */ }
b) while condition { /* code */ }
c) while (condition) -> { /* code */ }
d) while: (condition) { /* code */ }
2. How many times does the code inside a while loop execute if the condition is initially
false?
a) Once
b) Twice
c) Zero times
d) Infinite times
3. Which of the following is essential to prevent a while loop from running indefinitely?
a) A break statement
b) An increment or decrement operation within the loop
c) A return statement
d) An else statement
4. What is an infinite loop in C#?
a) A loop that runs a fixed number of times
b) A loop that never starts
c) A loop that doesn't have a body
d) A loop that runs indefinitely
5. What will be the output of the following code?
int i = 1;
while (i <= 3)
{
Console.WriteLine(i);
i++;
}
a) 1 2 3
b) 1 2
c) 1 2 3 4
d) The loop does not execute
6. Is it possible to exit a while loop without meeting the condition in C#?
a) Yes, using the break statement
b) No, the condition must be met
c) Yes, using the continue statement
d) No, while loops cannot be exited prematurely
7. What happens if the increment statement in a while loop is accidentally omitted?
a) The loop will execute once
b) The loop will terminate immediately
c) The loop may become an infinite loop
d) A compile-time error will occur
8. Which of the following statements is true about the while loop in C#?
a) The while loop is a pre-test loop
b) The while loop is a post-test loop
c) The condition in a while loop can only be a boolean variable
d) The while loop cannot be used for iteration
9. How can you skip the current iteration in a while loop in C#?
a) Using the break statement
b) Using the continue statement
c) Using the pass statement
d) Using the skip statement
10. Which of the following is a valid while loop in C#?
a) while true { /* code */ }
b) while (true) { /* code */ }
c) while (true) -> { /* code */ }
d) while: (true) { /* code */ }
11. What is the role of the condition in a while loop?
a) It determines how many times the loop will run
b) It determines whether the loop's code block will execute
c) It increments a counter variable
d) It is optional and can be omitted
12. Can a while loop contain multiple conditions in C#?
a) Yes, separated by commas
b) Yes, using logical operators like && and ||
c) No, it can only have a single condition
d) Yes, but only using bitwise operators
13. What happens when a return statement is executed inside a while loop?
a) The loop pauses and resumes after the function call
b) The loop continues to the next iteration
c) The function containing the loop returns immediately
d) The loop exits and the rest of the function continues
14. In C#, what is the difference between a while loop and a do-while loop?
a) A while loop is a pre-test loop, and a do-while loop is a post-test loop
b) There is no difference; they function identically
c) A do-while loop cannot have a break statement
d) A while loop cannot be nested, but a do-while loop can
15. How can you ensure a while loop executes at least once in C#?
a) By setting the initial condition to true
b) By using a do-while loop instead
c) By placing the condition at the end of the loop
d) By initializing all variables outside the loop
C# Methods MCQ
1. How do you define a method in C#?
a) dataType methodName(parameters) { /* code */ }
b) method dataType methodName(parameters) { /* code */ }
c) dataType methodName = (parameters) { /* code */ }
d) function dataType methodName(parameters) { /* code */ }
2. What is the return type of a method that does not return any value?
a) void
b) null
c) None
d) Empty
3. How can you call a method in C#?
a) methodName.call(parameters);
b) methodName(parameters);
c) Call methodName(parameters);
d) Execute methodName(parameters);
4. What are parameters in a C# method?
a) Variables passed into the method
b) The return values of the method
c) Internal variables of the method
d) External requirements for the method
5. Which keyword is used to indicate that a method parameter is optional in C#?
a) optional
b) default
c) params
d) OptionalParameter
6. What is method overloading in C#?
a) Changing the return type of a method
b) Creating a method with the same name but different parameters
c) Increasing the efficiency of a method
d) Writing the method code in multiple languages
7. What is the correct way to declare a method that returns an integer in C#?
a) int methodName() { /* code */ }
b) method int methodName() { /* code */ }
c) function int methodName() { /* code */ }
d) methodName() : int { /* code */ }
8. How are arguments passed to a method by default in C#?
a) By reference
b) By value
c) By pointer
d) By object
9. What keyword is used to define a method that belongs to a class rather than an
instance of a class in C#?
a) static
b) const
c) class
d) shared
10. How can you return multiple values from a method in C#?
a) Using a struct or class
b) Returning an array or a list
c) Using out or ref parameters
d) All of the above
11. What does the 'ref' keyword do in a C# method signature?
a) Specifies a reference type parameter
b) Indicates that a parameter is optional
c) Allows the method to modify the argument's value
d) Signals a recursive method
12. What is recursion in C#?
a) A method that calls itself
b) Repeating a method multiple times
c) Looping through method calls
d) Overloading a method
13. What is a constructor in C#?
a) A method that constructs data types
b) A static method that initializes a class
c) A special method used to initialize objects
d) A method that is called when an object is destroyed
14. Can a C# method return null?
a) Yes, if the method's return type is a reference type or a nullable value type
b) No, methods in C# cannot return null
c) Yes, but only for string return types
d) No, unless explicitly allowed by the 'null' keyword
15. What is the difference between 'ref' and 'out' parameters in C#?
a) 'ref' parameters must be initialized before being passed; 'out' parameters do
not
b) 'out' parameters can be passed by value
c) 'ref' parameters are for input only; 'out' parameters are for output only
d) There is no difference
C# OOP MCQ
1. What is encapsulation in C#?
a) Breaking a program into multiple parts
b) Hiding the internal state of an object and requiring all interaction to be
performed through an object's methods
c) The ability of a class to extend the functionality of another class
d) A class that can be used to create objects
2. What does the 'public' keyword denote in C#?
a) The method can only be accessed within its class
b) The variable is a constant
c) The method or property is accessible from any class
d) The method is static
3. What is a class in C#?
a) A blueprint from which objects are created
b) A specific instance of an object
c) A method in object-oriented programming
d) A variable type
4. What is polymorphism in C#?
a) The ability of different classes to use methods of the same name in different
ways
b) Splitting a class into multiple subclasses
c) Changing the data type of a variable
d) Protecting data from access by unauthorized code
5. What is inheritance in C#?
a) Copying properties from one class to another
b) The process where one class acquires the properties and behaviours of
another class
c) Using the same method in multiple classes
d) Storing data within a class
6. What is the purpose of constructors in C#?
a) To initialize objects of a class
b) To perform calculations
c) To connect to a database
d) To destroy objects of a class
7. What is an object in C#?
a) A variable in a class
b) An instance of a class
c) A method in a class
d) A property in a class
8. What is an abstract class in C#?
a) A class that cannot be instantiated and must be inherited
b) A class designed for creating objects
c) A class without any methods
d) A class used for data storage only
9. What is the purpose of the 'this' keyword in C#?
a) To reference the current instance of a class
b) To destroy the current instance of a class
c) To create a new instance of a class
d) To exit the current method
10. What does the term 'interface' refer to in C#?
a) A class that provides a default implementation of methods
b) A protocol that defines a set of methods and properties without implementing
them
c) A method that connects two classes
d) A built-in data type
11. What is method overloading in C#?
a) Changing the return type of a method
b) Creating multiple methods with the same name but different parameters in
the same class
c) Using the same method name in different classes
d) Creating a method that loads data
12. What is the use of the 'virtual' keyword in C#?
a) To declare a variable that can change types
b) To mark a method or property as overridable in a derived class
c) To create a virtual class
d) To define a method that must be implemented in a derived class
13. What is method overriding in C#?
a) Renaming a method in a derived class
b) Providing a new implementation of a method in a derived class that already
exists in the base class
c) Overloading a method in the same class
d) Changing the return type of a method in a derived class
14. What does the 'sealed' keyword do in C#?
a) Seals a method so it cannot be overridden
b) Seals a class so it cannot be extended
c) Protects a variable from being modified
d) Encrypts data within a class
15. What is a destructor in C#?
a) A method for destroying classes
b) A special method used to clean up before an object is garbage collected
c) A method used to delete variables
d) An overloaded version of a constructor
16. What is a property in C#?
a) A global variable in a class
b) A special type of class
c) A method that does not take any parameters
d) A member that provides a flexible mechanism to read, write, or compute the
value of a private field
17. What is the difference between a class and a struct in C#?
a) A class is a reference type, while a struct is a value type
b) There is no difference
c) Structs can have methods, while classes cannot
d) Classes are used for small objects, while structs are used for large objects
18. What is an 'interface' in C#?
a) A collection of abstract methods
b) A class that cannot be instantiated
c) A protocol that defines a set of methods and properties without implementing
them
d) A built-in data type
19. What is the purpose of the 'override' keyword in C#?
a) To replace a method in a base class with a new implementation in a derived
class
b) To prevent a method from being overridden
c) To make a method available for overloading
d) To indicate that a method is optional to implement
20. What does 'encapsulation' achieve in C# OOP?
a) It ensures that only necessary aspects of a class are exposed to other parts of
the program
b) It allows a class to inherit from multiple base classes
c) It provides a mechanism to copy objects
d) It allows a method to change its behavior based on the input
C# Inheritance MCQ
1. What is inheritance in C#?
a) The process of creating new classes from existing ones
b) Copying the same method into multiple classes
c) Using the same variables in different classes
d) Connecting classes through interfaces
2. What keyword is used to inherit from a base class in C#?
a) extends
b) super
c) base
d) :
3. Which class in C# is the base class for all other classes?
a) Object
b) Base
c) Root
d) Main
4. Can a derived class override a non-virtual method of the base class in C#?
a) Yes, always
b) No, only virtual methods can be overridden
c) Yes, but only if the method is static
d) No, unless the method is abstract
5. What is the correct way to call a constructor of the base class in C#?
a) super()
b) base()
c) :base()
d) parent()
6. What is the default access modifier for base class inheritance in C#?
a) public
b) private
c) protected
d) internal
7. Can structures (structs) in C# inherit from other structures or classes?
a) Yes, from both structures and classes
b) No, structures cannot inherit from any type
c) Yes, but only from other structures
d) Yes, but only from classes
8. What is the purpose of the 'sealed' keyword in relation to inheritance in C#?
a) To prevent a class from inheriting from other classes
b) To allow a class to be inheritable
c) To prevent a class from being a base class
d) To seal methods so they cannot be overridden
9. What is multiple inheritance in the context of C#?
a) Inheriting from multiple base classes
b) Inheriting both methods and properties
c) A class inheriting from another class and multiple interfaces
d) A concept not supported in C#
10. Can a derived class in C# access private members of its base class?
a) Yes, always
b) No, private members are accessible only within the class they are declared
c) Yes, but only through public or protected methods
d) No, unless the class is sealed
C# Polymorphism MCQ
1. What is polymorphism in C#?
a) A way to process different data types with a single interface
b) The ability of different classes to provide unique implementations of the same
method
c) A feature that allows a class to have multiple constructors
d) The ability to change a variable's type at runtime
2. Which keyword is used for method overriding in C#?
a) virtual
b) override
c) new
d) base
3. What is method overloading in C#?
a) Creating methods in a derived class with the same name as the base class
b) Defining multiple methods with the same name but different parameters
within the same class
c) Changing the return type of a method
d) Calling methods from the base class
4. What is required to make a method overridable in a base class in C#?
a) The method must be static
b) The method must be private
c) The method must be declared with the 'virtual' keyword
d) The method must be sealed
5. How can you hide a method in a base class without overriding it in C#?
a) Using the 'new' keyword
b) Using the 'hide' keyword
c) By making the method private
d) By making the method static
6. What is dynamic polymorphism in C#?
a) Polymorphism determined at compile time
b) Polymorphism that occurs when code is executed
c) Polymorphism achieved through method overloading
d) Polymorphism achieved with dynamic types
7. What happens when a virtual method is called on a reference whose compile-time
type is different from its runtime type?
a) The method of the compile-time type is executed
b) The method of the runtime type is executed
c) A runtime error occurs
d) The method call is ignored
8. Can interfaces be used to achieve polymorphism in C#?
a) No, interfaces cannot be used for polymorphism
b) Yes, by allowing different classes to implement the same interface differently
c) Only if the interface methods are static
d) Only within the same assembly
9. What is the difference between static and dynamic polymorphism?
a) Static polymorphism is compile-time, while dynamic polymorphism is runtime
b) Static polymorphism uses virtual methods, while dynamic polymorphism uses new
methods
c) There is no difference
d) Static polymorphism uses inheritance, while dynamic polymorphism uses
interfaces
10. In C#, how do you prevent a method from being overridden in a derived class?
a) By declaring the method as private
b) By using the 'final' keyword
c) By using the 'sealed' keyword on the method
d) By omitting the 'virtual' keyword
C# Enum MCQ
1. What is an enum in C#?
a) A method that returns several values
b) A user-defined data type that consists of integral constants
c) A special type of class for storing collections
d) A data structure similar to an array
2. What is the default underlying type of an enum in C#?
a) int
b) short
c) long
d) byte
3. How can you define an enum in C# with a different underlying type?
a) enum MyEnum : byte { Value1, Value2 }
b) enum MyEnum { Value1, Value2 } as byte
c) byte enum MyEnum { Value1, Value2 }
d) enum byte MyEnum { Value1, Value2 }
4. Can an enum in C# contain duplicate values?
a) Yes
b) No
c) Only if they are of different types
d) Only if they are explicitly allowed
5. How do you assign a specific value to an enum member in C#?
a) enum MyEnum { Value1 = 1, Value2 = 2 }
b) enum MyEnum { Value1(1), Value2(2) }
c) enum MyEnum { 1, 2 }
d) enum MyEnum { Value1, Value2 } = { 1, 2 }
6. What is the first value of an enum in C# if no value is explicitly assigned?
a) 0
b) 1
c) null
d) It generates a compile-time error
7. How can you convert an enum to its corresponding numeric value?
a) (int)MyEnum.Value1
b) Convert.ToInt32(MyEnum.Value1)
c) MyEnum.Value1.ToInt()
d) Both a and b
8. How do you define a flag enum in C#?
a) enum MyEnum { Value1, Value2 } with flags
b) enum MyEnum { Value1 = 1, Value2 = 2 } as flags
c) [Flags] enum MyEnum { Value1 = 1, Value2 = 2 }
d) enum MyEnum : Flags { Value1, Value2 }
9. Can you use strings as enum values in C#?
a) Yes
b) No
c) Only with the [Strings] attribute
d) Only in a special type of enum
10. What happens when you try to parse an invalid string to an enum in C#?
a) It returns null
b) It defaults to the first enum value
c) It throws an exception
d) It ignores the invalid value
C# tutorialspoint MCQ
1. Which of the following is correct about C#?
a) C# is a modern, general-purpose, object-oriented programming language developed
by Microsoft.
b) C# was developed by Anders Hejlsberg and his team during the development of .Net
Framework.
c) C# is designed for Common Language Infrastructure (CLI).
d) All of the above.
2. Which of the following is correct about C#?
a) It is component oriented.
b) It can be compiled on a variety of computer platforms.
c) It is a part of .Net Framework.
d) All of the above.
3. Which of the following keyword is used for including the namespaces in the program
in C#?
a) imports
b) using
c) exports
d) None of the above.
4. Which of the following is correct about variable naming conventions in C#?
a) A name must begin with a letter that could be followed by a sequence of letters, digits
(0 - 9) or underscore.
b) The first character in an identifier cannot be a digit.
c) Both of the above.
d) None of the above.
5. Which of the following is correct about variable naming conventions in C#?
a) It should not be a C# keyword.
b) It must not contain any embedded space or symbol such as? - + ! @ # % ^ & * ( ) [ ] {
}
c) Both of the above.
d) None of the above.
6. Which of the following is a reserved keyword in C#?
a) abstract
b) as
c) foreach
d) All of the above.
7. Which of the following is a contextual keyword in C#?
a) get
b) set
c) add
d) All of the above.
8. We can use reserved keywords as identifiers in C#?
a) true
b) false
9. We can use reserved keywords as identifiers in C# by prefixing them with @
character?
a) true
b) false
10. Which of the following variable types can be assigned a value directly in C#?
a) Value types
b) Reference types
c) Pointer types
d) All of the above.
11. Value type variables in C# are derived from the class System.ValueType?
a) true
b) false
12. Which of the following is correct about value type variables in C#?
a) The value types directly contain data.
b) int, char, and float, which stores numbers, alphabets, and floating point numbers,
respectively are value types.
c) When you declare an int type, the system allocates memory to store the value.
d) All of the above.
13. Which of the following is correct about reference type variables in C#?
a) The reference types do not contain the actual data stored in a variable.
b) They contain a reference to the variables.
c) Example of built-in reference types are: object, dynamic, and string.
d) All of the above.
14. Which of the following is correct about Object Type in C#?
a) The Object Type is the ultimate base class for all data types in C# Common Type
System (CTS).
b) Object is an alias for System.Object class.
c) The object types can be assigned values of any other types, value types, reference
types, predefined or user-defined types.
d) All of the above.
15. Which of the following defines boxing correctly?
a) When a value type is converted to object type, it is called boxing.
b) When an object type is converted to a value type, it is called boxing.
c) Both of the above.
d) None of the above.
16. Which of the following defines unboxing correctly?
a) When a value type is converted to object type, it is called unboxing.
b) When an object type is converted to a value type, it is called unboxing.
c) Both of the above.
d) None of the above.
17. Which of the following is correct about dynamic Type in C#?
a) You can store any type of value in the dynamic data type variable.
b) Type checking for these types of variables takes place at run-time.
c) Both of the above.
d) None of the above.
18. Which of the following converts a type to a Boolean value, where possible in C#?
a) ToBoolean
b) ToSingle
c) ToChar
d) ToDateTime
19. Which of the following converts a type to a byte value in C#?
a) ToSingle
b) ToByte
c) ToChar
d) ToDateTime
20. Which of the following converts a type to a single Unicode character, where possible
in C#?
a) ToSingle
b) ToByte
c) ToChar
d) ToDateTime
21. Which of the following converts a type (integer or string type) to date-time
structures in C#?
a) ToString
b) ToSingle
c) ToChar
d) ToDateTime
22. Which of the following converts a floating point or integer type to a decimal type in
C#?
a) ToDecimal
b) ToDouble
c) ToInt16
d) ToInt32
23. Which of the following converts a type to a double type in C#?
a) ToDecimal
b) ToDouble
c) ToInt16
d) ToInt32
24. Which of the following converts a type to a 16-bit integer in C#?
a) ToDecimal
b) ToDouble
c) ToInt16
d) ToInt32
25. Which of the following converts a type to a 32-bit integer in C#?
a) ToDecimal
b) ToDouble
c) ToInt16
d) ToInt32