C# (pronounced “C-sharp”) is a powerful, versatile programming language developed by Microsoft that runs on the .NET platform.
This page offers a compilation of 50 carefully curated Multiple Choice Questions (MCQs) on C#. These questions cover a broad range of topics, from the basics to more advanced concepts, and are designed to challenge your understanding of the language.
Whether you’re preparing for a job interview, a certification exam, or just looking to test your C# knowledge, these MCQs are an invaluable resource. Read on to see how many you can answer correctly, and don’t forget to check the explanations for insights and clarifications!
1. Which namespace is fundamental for basic C# operations?
Answer:
Explanation:
The System namespace contains fundamental classes and base classes that define commonly used value and reference data types, events and event handlers, interfaces, attributes, and processing exceptions.
2. What is the Console.WriteLine() function do?
Answer:
Explanation:
The Console.WriteLine() function is used to display a message on the console and then move to the next line.
3. Which keyword is used to create an instance of a class?
Answer:
Explanation:
The new keyword is used to create an instance of a class, also known as an object.
4. Which of the following data types can store a non-integer number?
Answer:
Explanation:
The double data type is used to store floating-point numbers (numbers with decimal points).
5. What does the static keyword mean in C#?
Answer:
Explanation:
A static member belongs to the class/type itself rather than a specific instance.
6. What is the purpose of the Main method in C#?
Answer:
Explanation:
The Main method is the primary entry point for a C# console application.
7. Which of the following is NOT a valid access modifier in C#?
Answer:
Explanation:
transient is not a valid access modifier in C#. It’s used in Java for other purposes.
8. What is encapsulation?
Answer:
Explanation:
Encapsulation refers to the bundling of data and methods that operate on that data within a single unit, typically a class.
9. Which keyword is used to inherit from a base class in C#?
Answer:
Explanation:
In C#, the : symbol is used to indicate inheritance from a base class or implementation of an interface.
10. Which type of exception handling uses a try-catch block?
Answer:
Explanation:
Checked exceptions are handled using try-catch blocks. These are the exceptions that are checked at compile time.
11. Which of the following is NOT a loop structure in C#?
Answer:
Explanation:
C# does not have a loop structure called loop.
12. Which operator is used for type casting in C#?
Answer:
Explanation:
The as operator is used for type casting in C#. If the conversion is not possible, it returns null.
13. Which of the following is the correct way to comment out multiple lines in C#?
Answer:
Explanation:
The /* … */ syntax is used to comment out multiple lines in C#.
14. Which keyword is used to declare a constant variable in C#?
Answer:
Explanation:
In C#, the const keyword is used to declare a variable as constant, meaning its value cannot be changed after initialization.
15. Which of the following data types can store a Unicode character in C#?
Answer:
Explanation:
The char data type in C# is used to store a single Unicode character.
16. How do you define a method in C# that does not return a value?
Answer:
Explanation:
In C#, the void keyword indicates that a method does not return a value.
17. Which of the following statements about C# interfaces is TRUE?
Answer:
Explanation:
Interfaces in C# can only declare methods, properties, events, or indexers without providing the actual implementation.
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?
Answer:
Explanation:
The internal access modifier allows a member to be accessible from any class within the same assembly, but not from outside that assembly.
19. Which of the following is NOT a type of constructor in C#?
Answer:
Explanation:
There’s no concept of a “Derived constructor” in C#. Constructors in derived classes are just constructors that can call the base class constructor.
20. What does the ?? operator do in C#?
Answer:
Explanation:
The ?? is called the null-coalescing operator. It returns the left-hand operand if it is not null; otherwise, it returns the right-hand operand.
21. What is boxing in C#?
Answer:
Explanation:
Boxing is the process of converting a value type to the type object or any interface type implemented by this value type.
22. What does the override keyword do in C#?
Answer:
Explanation:
The override keyword is used in a derived class to provide a new implementation for a method that is declared as virtual in the base class.
23. Which collection in C# does NOT allow duplicate elements?
Answer:
Explanation:
HashSet is designed to store unique elements, and it doesn’t allow duplicates.
24. How do you declare a single-dimensional array in C#?
Answer:
Explanation:
The correct way to declare a single-dimensional array in C# is int[] x;.
25. What is LINQ in C#?
Answer:
Explanation:
LINQ (Language Integrated Query) is a Microsoft .NET Framework component that adds native data querying capabilities to .NET languages.
26. Which of the following statements correctly initializes a string to an empty value?
Answer:
Explanation:
Both b) and d) initialize the string to an empty value, but String.Empty is a more readable way of representing an empty string in C#.
27. What does the finally block in exception handling do?
Answer:
Explanation:
The finally block is used to execute code, regardless of whether an exception is thrown or caught. It is typically used for cleanup activities, such as closing files or releasing resources.
28. Which of the following keywords is used to declare a constant variable?
Answer:
Explanation:
The const keyword in C# is used to declare a constant variable. Once assigned, its value cannot be changed.
29. Which of the following methods is used to determine the length of a string in C#?
Answer:
Explanation:
In C#, the Length property of the string class is used to get the number of characters in a string.
30. What is the base class for all classes in C#?
Answer:
Explanation:
The Object class is the base class for all classes in C#.
31. Which of the following is NOT a valid access modifier in C#?
Answer:
Explanation:
secured is not a valid access modifier in C#. The valid access modifiers are public, private, protected, internal, and a few combinations thereof.
32. Which of the following types is a reference type?
Answer:
Explanation:
string is the reference type in C#.
33. In C#, what does the is keyword do?
Answer:
Explanation:
The is keyword is used in C# to check if an object is of a certain type or compatible with that type.
34. How can you make a class in C# so that it cannot be inherited?
Answer:
Explanation:
In C#, the sealed keyword prevents a class from being inherited.
35. Which of the following is the correct way to define an anonymous method in C#?
Answer:
Explanation:
Anonymous methods in C# are created using the delegate keyword followed by the method body.
36. Which of the following correctly declares a nullable int in C#?
Answer:
Explanation:
In C#, the ? modifier is used to declare a nullable value type.
37. Which of the following is NOT a valid C# collection?
Answer:
Explanation:
While LinkedList is a valid collection in C#, there’s no collection named LinkList.
38. Which method is called to free the resources of an object manually in C#?
Answer:
Explanation:
The Dispose() method is used to release unmanaged resources in C#.
39. Which of the following keywords is used to handle an exception in C#?
Answer:
Explanation:
In C#, the try-catch block is used for exception handling, where the catch block catches and handles exceptions.
40. What does the as keyword do in C#?
Answer:
Explanation:
The as keyword in C# performs a type conversion, but instead of throwing an exception, it returns null if the conversion isn’t possible.
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?
Answer:
Explanation:
In C#, the int data type is a 32-bit signed integer that can range from -2,147,483,648 to 2,147,483,647.
42. Which of the following keywords is used to declare an abstract class in C#?
Answer:
Explanation:
The abstract keyword is used in C# to declare an abstract class. Abstract classes cannot be instantiated.
43. Which of the following methods can be overridden in a derived class?
Answer:
Explanation:
In C#, only methods declared with the virtual keyword can be overridden in a derived class.
44. What is the default value of a bool in C#?
Answer:
Explanation:
In C#, the default value for a bool (boolean) data type is false.
45. Which collection in C# ensures that all elements are unique?
Answer:
Explanation:
The HashSet collection in C# ensures that all elements are unique. It does not allow duplicate values.
46. Which operator can be used to determine the type of an object at runtime?
Answer:
Explanation:
The typeof operator is used to obtain the Type object for a type. However, to check an object’s type at runtime, the is operator is more commonly used.
47. Which of the following statements about delegates in C# is true?
Answer:
Explanation:
Delegates in C# are type-safe. They ensure the method signature matches the delegate signature. Delegates can point to both instance and static methods and can also be used with lambda expressions.
48. Which of the following provides a way to have multiple implementations for a method?
Answer:
Explanation:
Method overloading allows a class to have multiple methods with the same name but with different parameters.
49. What does the virtual keyword indicate?
Answer:
Explanation:
The virtual keyword in C# indicates that a method, property, indexer, or event can be overridden in derived classes.
50. Which of the following statements correctly creates an array of 5 integers?
Answer:
Explanation:
In C#, arrays are declared by specifying the type of the elements followed by square brackets. They are then instantiated using the new keyword followed by the type and the size of the array.