100% found this document useful (1 vote)
180 views

C# MCQ 2024

Uploaded by

rajaduraicse1993
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
180 views

C# MCQ 2024

Uploaded by

rajaduraicse1993
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 47

1. Which namespace is fundamental for basic C# operations?

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.

57. Which of the following is the correct about class destructor?


a) A destructor is a special member function of a class that is executed whenever an
object of its class goes out of scope.
b) A destructor has exactly the same name as that of the class with a prefixed tilde (~)
and it can neither return a value nor can it take any parameters.
c) Both of the above.
d) None of the above.
58. Dynamic polymorphism is implemented by abstract classes and virtual functions.
a) true
b) false
59. Which of the following preprocessor directive allows creating a compound
conditional directive in C#?
a) elif
b) define
c) if
d) else
60. Which of the following is true about System.ApplicationException class in C#?
a) The System.ApplicationException class supports exceptions generated by application
programs.
b) Exceptions defined by the programmers should derive from this class.
c) Both of the above.
d) None 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# Data Types MCQ


1. Which of the following is a value type in C#?
a) String
b) Class
c) Struct
d) Delegate
2. What is the size of a 'long' data type in C#?
a) 8 bytes
b) 4 bytes
c) 2 bytes
d) 16 bytes
3. Which data type would you use in C# to store a large, precise decimal number?
a) float
b) double
c) decimal
d) long
4. What is the range of the 'double' data type in C#?
a) ±1.7E±308 (15 digits)
b) ±3.4E±38 (7 digits)
c) ±5.0E±324 (28 digits)
d) ±1.0E±28 (7 digits)
5. What is the default value of a reference type in C#?
a) 0
b) false
c) null
d) 0.0
6. Which of the following is a non-nullable value type in C#?
a) int?
b) string
c) bool?
d) float
7. How many bytes are used by a 'char' in C#?
a) 1
b) 2
c) 4
d) 8
8. Which of the following data types is suitable for storing a single letter or character in
C#?
a) String
b) Char
c) Byte
d) Single
9. In C#, what is the maximum value that an 'int' data type can hold?
a) 2,147,483,647
b) 4,294,967,295
c) 32,767
d) 65,535
10. What is the size of a 'bool' data type in C#?
a) 1 bit
b) 1 byte
c) 2 bytes
d) 4 bytes
11. Which data type in C# is best suited for storing a small integer value?
a) int
b) short
c) long
d) byte
12. What is the default value of a struct in C#?
a) null
b) 0
c) False
d) The default value of each individual field
13. What is the difference between 'string' and 'String' in C#?
a) 'string' is a value type, while 'String' is a reference type.
b) 'string' is a reference type, while 'String' is a value type.
c) 'string' is an alias for 'System.String'.
d) There is no difference; they are interchangeable.
14. In C#, what does the 'uint' data type represent?
a) An unsigned 32-bit integer
b) A signed 32-bit integer
c) An unsigned 16-bit integer
d) A signed 64-bit integer
15. Which of the following data types has the smallest range in C#?
a) byte
b) sbyte
c) short
d) ushort
16. What is the purpose of the 'dynamic' data type in C#?
a) To store any type of data that is known at compile-time.
b) To delay type checking until runtime.
c) To create anonymous types.
d) To define a variable that cannot change its data type.
17. What is the output of the following C# code?
byte a = 255;
a++;
a) 0
b) 255
c) 256
d) An error
18. Which C# data type should be used for a variable that needs to hold a large number
of true/false values?
a) bool
b) BitArray
c) int
d) bool[]
19. What is the range of the 'float' data type in C#?
a) ±1.5E−45 to ±3.4E38
b) ±5.0E−324 to ±1.7E308
c) ±1.0E−28 to ±7.9E28
d) ±1.0E−20 to ±1.0E20
20. In C#, which data type is implicitly nullable?
a) int
b) char
c) string
d) bool
C# Operators MCQ
1. What is the output of the following expression in C#?
5 + 3 * 2
a) 16
b) 11
c) 8
d) 13
2. In C#, which operator is used for concatenating two strings?
a) +
b) &
c) .
d) *
3. What is the purpose of the '??' operator in C#?
a) It checks if the value is null and returns an alternative if true.
b) It adds two nullable types.
c) It compares two values for equality.
d) It performs a logical OR operation.
4. Which operator in C# is used to check type compatibility?
a) is
b) typeof
c) as
d) ==
5. What does the following C# code return?
int x = 10;
int result = x++;
a) 10
b) 11
c) 9
d) 12
6. Which of the following is the correct use of the conditional (ternary) operator in C#?
a) int result = (x > 10) ? x : 10;
b) int result = x > 10 ? x : 10;
c) int result = if(x > 10) x; else 10;
d) int result = x > 10 : x ? 10;
7. What is the purpose of the '%' operator in C#?
a) Exponentiation
b) Division
c) Modulus
d) Multiplication
8. In C#, which operator is used for bitwise AND operation?
a) &&
b) &
c) ||
d) |
9. What will be the result of the expression (true || false) in C#?
a) true
b) false
c) null
d) 1
10. Which operator in C# is used to explicitly convert a type?
a) as
b) is
c) ()
d) typeof
11. How does the 'as' operator differ from casting in C#?
a) 'as' operator performs a deep copy of the object.
b) 'as' operator returns null if the conversion fails, whereas casting throws an
exception.
c) There is no difference.
d) 'as' operator is used for value types only.
12. What is the precedence of the logical AND ('&&') operator compared to the logical
OR ('||') operator in C#?
a) '&&' has higher precedence than '||'.
b) '||' has higher precedence than '&&'.
c) Both have the same precedence.
d) The precedence is determined at runtime.
13. Which of the following operators can be overloaded in C#?
a) +
b) ?:
c) &&
d) ??
14. What will be the result of the expression (5 & 3) in C#?
a) 6
b) 1
c) 7
d) 0
15. What is the output of the following C# code?
int a = 10;
int b = 3;
int result = a / b;
a) 3.3333
b) 3
c) 3.3
d) 4

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# For Loop MCQ


1. What is the basic syntax of a for loop in C#?
a) for (initialization; condition; iteration) { /* code */ }
b) for (condition; initialization; iteration) { /* code */ }
c) for (initialization; iteration; condition) { /* code */ }
d) for where (initialization; condition; iteration) { /* code */ }
2. Which part of a for loop is executed only once?
a) The condition
b) The iteration statement
c) The body of the loop
d) The initialization statement
3. What will be the output of the following code?
for (int i = 0; i < 3; i++)
{
Console.WriteLine(i);
}
a) 0 1 2
b) 0 1 2 3
c) 1 2 3
d) 1 2 3 4
4. Which part of the for loop is optional?
a) The condition
b) The initialization
c) The iteration
d) All of the above
5. How can you exit a for loop prematurely in C#?
a) Using the stop statement
b) Using the exit statement
c) Using the break statement
d) Using the return statement
6. What is the purpose of the continue statement in a for loop in C#?
a) To exit the loop
b) To skip the current iteration and continue with the next one
c) To repeat the current iteration
d) To pause the loop execution
7. What happens if you omit the iteration expression in a for loop?
a) The loop does not execute
b) The loop executes once
c) The loop becomes an infinite loop
d) A compile-time error occurs
8. Which type of loop is most appropriate for iterating over an array in C#?
a) While loop
b) Do-while loop
c) For loop
d) All of the above
9. In a for loop, is it possible to have multiple initialization statements?
a) Yes, separated by commas
b) No, only one statement is allowed
c) Yes, separated by semicolons
d) No, initialization must be done outside the loop
10. Can the condition in a for loop be a complex expression?
a) Yes, as long as it evaluates to a boolean value
b) No, it must be a simple true or false condition
c) Yes, but only with logical AND operators
d) No, complex conditions are not allowed in for loops
11. What is the scope of a variable declared in the initialization section of a for loop?
a) Global
b) Local to the loop
c) Local to the method containing the loop
d) Local to the class containing the loop
12. How many times does the body of a for loop execute if its condition is never true?
a) Once
b) Twice
c) Zero times
d) Infinite times
13. What is the correct syntax for a for loop with no body in C#?
a) for (initialization; condition; iteration);
b) for (initialization; condition; iteration) {}
c) for (initialization; condition; iteration) /;
d) for (initialization; condition; iteration) //;
14. Can you declare a variable of a different type in the iteration part of a for loop?
a) Yes
b) No, it must be of the same type as the initialization part
c) Yes, but only if it is a numeric type
d) No, the iteration part cannot have declarations
15. Is it possible to nest for loops in C#?
a) Yes, you can nest for loops within each other
b) No, for loops cannot be nested
c) Yes, but only two levels deep
d) Yes, but only if they iterate over different types

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# Access Modifiers MCQ


1. What does the 'public' access modifier do in C#?
a) Restricts access to the member within its declaring type
b) Allows access to the member from any other code
c) Allows access only within the same assembly
d) Provides global access to a variable
2. Which access modifier in C# is the most restrictive?
a) public
b) private
c) protected
d) internal
3. What is the default access modifier for a class in C#?
a) public
b) private
c) protected
d) internal
4. What does the 'protected' access modifier do in C#?
a) Allows access from any class
b) Restricts access to within the class and derived classes
c) Restricts access to the same assembly
d) Allows access only within the same namespace
5. How does the 'internal' access modifier work in C#?
a) Restricts access to within the current class
b) Allows access from any class within the same assembly
c) Allows access only from derived classes
d) Provides unrestricted access to the member
6. What is the combination of 'protected' and 'internal' called in C#?
a) protected internal
b) internal protected
c) private public
d) public private
7. What access modifier should be used to allow a member to be accessible only within
its declaring type?
a) public
b) private
c) protected
d) internal
8. Which access modifier allows a member to be accessed from derived classes, even in
different assemblies?
a) private
b) internal
c) protected internal
d) public
9. What is the default access modifier for a method in C#?
a) public
b) private
c) protected
d) internal
10. Can a 'private' member of a class in C# be accessed from an instance of that class?
a) Yes, from anywhere
b) No, never
c) Yes, but only within the class itself
d) Yes, but only from derived classes
Click to View Answer and Explanation
11. What is the effect of the 'protected internal' access modifier on a member in C#?
a) Accessible only within the same assembly and from derived classes in other
assemblies
b) Accessible from anywhere
c) Accessible only from derived classes
d) Accessible only within the same class
12. Which access modifier should be used for members that should be accessible only to
derived classes and classes within the same assembly?
a) private
b) protected
c) internal
d) protected internal
13. What does the 'public' access modifier indicate about a member's accessibility?
a) Restricted within the class
b) Restricted within the assembly
c) Accessible from any other code
d) Accessible only within derived classes
14. If a member is declared without any access modifier, which access level is applied by
default in C#?
a) public
b) private
c) internal
d) protected
15. What is the purpose of using different access modifiers in C#?
a) To optimize code performance
b) To control the scope of access to class members for encapsulation and security
c) To define different types of classes
d) To specify the return type of methods

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# File Handling MCQ


1. Which namespace is commonly used for file handling in C#?
a) System.IO
b) System.File
c) System.Data
d) System.Text
2. How do you create a text file in C#?
a) File.CreateText("filename.txt")
b) File.New("filename.txt")
c) File.Create("filename.txt")
d) FileStream.Create("filename.txt")
3. What class is used to read text from a file in C#?
a) FileReader
b) File.Read
c) StreamReader
d) TextReader
4. Which method is used to write text to a file in C#?
a) File.WriteText()
b) StreamWriter.Write()
c) File.AppendText()
d) File.WriteAllLines()
5. How do you append text to an existing file in C#?
a) File.Append("filename.txt", "text")
b) File.AppendText("filename.txt")
c) StreamWriter.Append("filename.txt", "text")
d) File.AppendAllText("filename.txt", "text")
6. What is the purpose of the FileStream class in C#?
a) To stream data to memory
b) To read and write to a network stream
c) To read from and write to files
d) To serialize objects
7. Which method checks for the existence of a file in C#?
a) File.Exists("filename.txt")
b) File.Check("filename.txt")
c) File.IsPresent("filename.txt")
d) Directory.Exists("filename.txt")
8. How do you copy a file in C#?
a) File.CopyTo("source.txt", "destination.txt")
b) File.Copy("source.txt", "destination.txt")
c) FileStream.Copy("source.txt", "destination.txt")
d) StreamWriter.Copy("source.txt", "destination.txt")
9. How do you delete a file in C#?
a) File.Delete("filename.txt")
b) File.Remove("filename.txt")
c) File.Erase("filename.txt")
d) FileStream.Delete("filename.txt")
10. Which class is used to read and write binary files in C#?
a) BinaryReader and BinaryWriter
b) FileStream
c) StreamReader and StreamWriter
d) BufferedStream
11. How do you move a file in C#?
a) File.MoveTo("source.txt", "destination.txt")
b) File.Move("source.txt", "destination.txt")
c) FileStream.Move("source.txt", "destination.txt")
d) StreamWriter.Move("source.txt", "destination.txt")
12. What is the use of the Path class in C#?
a) To handle file paths and directories
b) To create new paths in the file system
c) To encrypt file paths
d) To stream file paths
13. What exception is commonly associated with file handling errors in C#?
a) IOException
b) FileNotFoundException
c) FileAccessException
d) PathException
14. How can you read all lines of a text file into a string array in C#?
a) File.ReadAllLines("filename.txt")
b) StreamReader.ReadAllLines("filename.txt")
c) FileStream.ReadAllLines("filename.txt")
d) File.ReadLines("filename.txt")
15. Which method is used to read all text from a file in one go in C#?
a) File.ReadAllText("filename.txt")
b) StreamReader.ReadToEnd("filename.txt")
c) FileStream.ReadAll("filename.txt")
d) File.ReadAll("filename.txt")

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

1. Which of the following converts a type to a 64-bit integer in C#?


a) ToInt64
b) ToSbyte
c) ToSingle
d) ToInt32
2. Which of the following converts a type to a signed byte type in C#?
a) ToInt64
b) ToSbyte
c) ToSingle
d) ToInt32
3. Which of the following converts a type to a small floating point number in C#?
a) ToInt64
b) ToSbyte
c) ToSingle
d) ToInt32
4. Which of the following converts a type to a string in C#?
a) ToInt64
b) ToSbyte
c) ToSingle
d) ToString
5. Which of the following converts a type to a specified type in C#?
a) ToType
b) ToSbyte
c) ToSingle
d) ToString
6. Which of the following converts a type to an unsigned int type in C#?
a) ToType
b) ToUInt16
c) ToSingle
d) ToString
7. Which of the following converts a type to an unsigned long type in C#?
a) ToType
b) ToUInt16
c) ToUInt32
d) ToString
8. Which of the following converts a type to an unsigned big type in C#?
a) ToType
b) ToUInt16
c) ToUInt32
d) ToUInt64
9. Which of the following operator returns the size of a data type in C#?
a) sizeof
b) typeof
c) &</a>
d) *
10. Which of the following operator returns the type of a class in C#?
a) sizeof
b) typeof
c) &</a>
d) *
11. Which of the following operator returns the address of an variable in C#?
a) sizeof
b) typeof
c) &
d) *
12. Which of the following operator creates a pointer to a variable in C#?
a) sizeof
b) typeof
c) &
d) *
13. Which of the following operator represents a conditional operation in C#?
a) ?:
b) is
c) as
d) *
14. Which of the following operator determines whether an object is of a certain type in
C#?
a) ?:
b) is
c) as
d) *
15. Which of the following operator casts without raising an exception if the cast fails in
C#?
a) ?:
b) is
c) as
d) *
16. Which of the following statements is correct about encapsulation?
a) Encapsulation is defined as the process of enclosing one or more items within a
physical or logical package.
b) Encapsulation, in object oriented programming methodology, prevents access to
implementation details.
c) Abstraction allows making relevant information visible and encapsulation enables a
programmer to implement the desired level of abstraction.
d) All of the above.
17. Which of the following statements is correct about access specifiers in C#?
a) Encapsulation is implemented by using access specifiers.
b) An access specifier defines the scope and visibility of a class member.
c) Both of the above.
d) None of the above.
18. Which of the following access specifier in C# allows a class to expose its member
variables and member functions to other functions and objects?
a) Public
b) Private
c) Protected
d) Internal
19. Which of the following access specifier in C# allows a class to hide its member
variables and member functions from other functions and objects?
a) Public
b) Private
c) Protected
d) Internal
20. Which of the following access specifier in C# allows a child class to access the
member variables and member functions of its base class?
a) Public
b) Private
c) Protected
d) Internal
21. Which of the following access specifier in C# allows a class to expose its member
variables and member functions to other functions and objects in the current assembly?
a) Public
b) Private
c) Protected
d) Internal
22. 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
23. Which of the following method copies the actual value of an argument into the
formal parameter of the function?
a) Value parameters
b) Reference parameters
c) Output parameters
d) None of the above.
24. Which of the following method copies the reference to the memory location of an
argument into the formal parameter?
a) Value parameters
b) Reference parameters
c) Output parameters
d) None of the above.
25. Which of the following method helps in returning more than one value?
a) Value parameters
b) Reference parameters
c) Output parameters
d) None of the above.

1. Which of the following is correct about nullable types in C#?


a) C# provides a special data types, the nullable types, to which you can assign normal
range of values as well as null values.
b) You can assign true, false, or null in a Nullable<bool> variable.
c) You can store any value from -2,147,483,648 to 2,147,483,647 or null in a
Nullable<Int32> variable.
d) All of the above.
2. Which of the following is correct about null coalescing operator in C#?
a) The null coalescing operator is used with the nullable value types and reference types.
b) It is used for converting an operand to the type of another nullable (or not) value type
operand, where an implicit conversion is possible.
c) If the value of the first operand is null, then the operator returns the value of the
second operand, otherwise it returns the value of the first operand.
d) All of the above.
3. 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.
4. Which of the following property of Array class in C# checks whether the Array has a
fixed size?
a) IsFixedSize
b) IsStatic
c) Length
d) None of the above.
5. Which of the following property of Array class in C# checks whether the Array is
readonly?
a) IsFixedSize
b) IsReadOnly
c) Length
d) None of the above.
6. Which of the following property of Array class in C# gets a 32-bit integer, the total
number of elements in all the dimensions of the Array?
a) Rank
b) LongLength
c) Length
d) None of the above.
7. Which of the following property of Array class in C# gets a 64-bit integer, the total
number of elements in all the dimensions of the Array?
a) Rank
b) LongLength
c) Length
d) None of the above.
8.Which of the following property of Array class in C# gets the rank (number of
dimensions) of the Array?
a) Rank
b) LongLength
c) Length
d) None of the above.
9. Which of the following is true about C# structures?
a) Structures can have methods, fields, indexers, properties, operator methods, and
events.
b) Structures can have defined constructors, but not destructors.
c) You cannot define a default constructor for a structure. The default constructor is
automatically defined and cannot be changed.
d) All of the above.
10. Which of the following is true about C# structures?
a) Unlike classes, structures cannot inherit other structures or classes.
b) Structure members cannot be specified as abstract, virtual, or protected.
c) A structure can implement one or more interfaces.
d) All of the above.
11. Which of the following is true about C# structures vs C# classes?
a) Classes are reference types and structs are value types.
b) Structures do not support inheritance.
c) Structures cannot have default constructor
d) All of the above.
12. Which of the following is true about C# enumeration?
a) An enumerated type is declared using the enum keyword.
b) C# enumerations are value data type.
c) Enumeration contains its own values and cannot inherit or cannot pass inheritance.
d) All of the above.
13. Which of the following is the default access specifier of a class?
a) Private
b) Public
c) Protected
d) Internal
14. Which of the following is the default access specifier of a class member variable?
a) Private
b) Public
c) Protected
d) Internal
15. Which of the following is the default access specifier of a class member function?
a) Private
b) Public
c) Protected
d) Internal
16. Which of the following is the correct about class member functions?
a) A member function of a class is a function that has its definition or its prototype
within the class definition similar to any other variable.
b) It operates on any object of the class of which it is a member, and has access to all the
members of a class for that object.
c) Both of the above.
d) None of the above.
17. Which of the following is the correct about class member variables?
a) Member variables are the attributes of an object (from design perspective) and they
are kept private to implement encapsulation.
b) These private variables can only be accessed using the public member functions.
c) Both of the above.
d) None of the above.
18. Which of the following is the correct about class constructor?
a) A class constructor is a special member function of a class that is executed whenever
we create new objects of that class.
b) A constructor has exactly the same name as that of class and it does not have any
return type.
c) Both of the above.
d) None of the above.
19. Which of the following is the correct about class destructor?
a) A destructor is a special member function of a class that is executed whenever an
object of its class goes out of scope.
b) A destructor has exactly the same name as that of the class with a prefixed tilde (~)
and it can neither return a value nor can it take any parameters.
c) Both of the above.
d) None of the above.
20. Which of the following is the correct about static member variables of a class?
a) We can define class members variables as static using the static keyword.
b) When we declare a member of a class as static, it means no matter how many objects
of the class are created, there is only one copy of the static member.
c) Both of the above.
d) None of the above.
21. Which of the following is the correct about static member functions of a class?
a) You can also declare a member function as static.
b) Such functions can access only static variables.
c) The static functions exist even before the object is created.
d) All of the above.
22. C# supports multiple inheritance.
a) true
b) false
23. C# does not support multiple inheritance.
a) true
b) false
24. Function overloading is a kind of static polymorphism.
a) true
b) false
25. Operator overloading is a kind of dynamic polymorphism.
a) true
b) false
1. Dynamic polymorphism is implemented by abstract classes and virtual functions.
a) true
b) false
2. The comparison operators can be overloaded.
a) true
b) false
3. The conditional logical operators can be overloaded.
a) true
b) false
4. The assignment operators cannot be overloaded.
a) true
b) false
5. Which of the following is the correct about interfaces in C#?
a) Interfaces are declared using the interface keyword.
b) Interface methods are public by default.
c) Both of the above.
d) None of the above.
6. Which of the following is the correct about namespaces in C#?
a) A namespace is designed for providing a way to keep one set of names separate from
another.
b) The class names declared in one namespace does not conflict with the same class
names declared in another.
c) The using keyword states that the program is using the names in the given namespace.
d) All of the above.
7. You can define one namespace inside another namespace.
a) true
b) false
8. Which of the following preprocessor directive defines a sequence of characters as
symbol in C#?
a) define
b) undef
c) region
d) endregion
9. Which of the following preprocessor directive allows you to undefine a symbol in C#?
a) define
b) undef
c) region
d) endregion
10. Which of the following preprocessor directive allows testing a symbol or symbols to
see if they evaluate to true in C#?
a) define
b) undef
c) if
d) elif
11. Which of the following preprocessor directive allows to create a compound
conditional directive, along with #if in C#?
a) define
b) elif
c) if
d) else
12. Which of the following preprocessor directive allows creating a compound
conditional directive in C#?
a) elif
b) define
c) if
d) else
13. Which of the following preprocessor directive specifies the end of a conditional
directive in C#?
a) elif
b) endif
c) if
d) else
14. Which of the following preprocessor directive lets you modify the compiler's line
number and (optionally) the file name output for errors and warnings in C#?
a) elif
b) endif
c) line
d) region
15. Which of the following preprocessor directive allows generating an error from a
specific location in your code in C#?
a) define
b) region
c) line
d) error
16. Which of the following preprocessor directive allows generating a level one warning
from a specific location in your code in C#?
a) warning
b) region
c) line
d) error
17. Which of the following preprocessor directive lets you specify a block of code that
you can expand or collapse when using the outlining feature of the Visual Studio Code
Editor in C#?
a) warning
b) region
c) line
d) error
18. Which of the following preprocessor directive marks the end of a #region block in
C#?
a) warning
b) region
c) endregion
d) error
19. Which of the following is true about try block in C#?
a) A try block identifies a block of code for which particular exceptions is activated.
b) It is followed by one or more catch blocks.
c) Both of the above.
d) None of the above.
20. Which of the following is true about catch block in C#?
a) A program catches an exception with an exception handler at the place in a program
where you want to handle the problem.
b) The catch keyword indicates the catching of an exception.
c) Both of the above.
d) None of the above.
21. The finally block is used to execute a given set of statements, whether an exception is
thrown or not thrown.
a) true
b) false
22. Which of the following is true about exceptions in C#?
a) The exception classes in C# are mainly directly or indirectly derived from the
System.Exception class.
b) C# exceptions are represented by classes.
c) Both of the above.
d) None of the above.
23. Which of the following is true about System.ApplicationException class in C#?
a) The System.ApplicationException class supports exceptions generated by application
programs.
b) Exceptions defined by the programmers should derive from this class.
c) Both of the above.
d) None of the above.
24. The System.SystemException class is the base class for all predefined system
exception in C#?
a) true
b) false
25. User-defined exception classes are derived from the ApplicationException class in
C#?
a) true
b) false

You might also like