C# MCQ

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?

a) using CSharp;
b) using System.Collections;
c) using System;
d) using Base;

Answer:

c) using System;

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?

a) Reads input
b) Writes to a file
c) Writes to the console with a new line
d) Clears the console

Answer:

c) Writes to the console with a new line

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?

a) class
b) new
c) struct
d) this

Answer:

b) new

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?

a) int
b) char
c) double
d) bool

Answer:

c) double

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#?

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

Answer:

c) The member belongs to the type rather than any specific instance

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#?

a) To define variables
b) To serve as the entry point for the application
c) To close the application
d) To initialize objects

Answer:

b) To serve as the entry point for the application

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#?

a) private
b) public
c) transient
d) protected

Answer:

c) transient

Explanation:

transient is not a valid access modifier in C#. It’s used in Java for other purposes.

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

Answer:

b) Wrapping up data members and methods into a single unit

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#?

a) extends
b) inherits
c) implement
d) :

Answer:

d) :

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?

a) Unchecked
b) Implicit
c) Explicit
d) Checked

Answer:

d) Checked

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#?

a) for
b) while
c) loop
d) foreach

Answer:

c) loop

Explanation:

C# does not have a loop structure called loop.

12. Which operator is used for type casting in C#?

a) ::
b) as
c) cast
d) ->

Answer:

b) as

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#?

a) // …
b) /* … */
c) — … —
d)

Answer:

b) /* … */

Explanation:

The /* … */ syntax is used to comment out multiple lines in C#.

14. Which keyword is used to declare a constant variable in C#?

a) constant
b) con
c) const
d) final

Answer:

c) const

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#?

a) char
b) string
c) byte
d) short

Answer:

a) char

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?

a) void
b) null
c) empty
d) returnless

Answer:

a) void

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?

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.

Answer:

d) They can declare methods without implementations.

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?

a) private
b) public
c) protected
d) internal

Answer:

d) internal

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#?

a) Copy constructor
b) Static constructor
c) Default constructor
d) Derived constructor

Answer:

d) Derived constructor

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#?

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

Answer:

c) Checks for nullability and returns the non-null value

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#?

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

Answer:

a) Placing a value type inside an object

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#?

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

Answer:

c) Provides a new implementation for a virtual method in a derived class

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?

a) List
b) Array
c) Hashtable
d) HashSet

Answer:

d) HashSet

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#?

a) int x[];
b) int x();
c) int[] x;
d) int{} x;

Answer:

c) int[] x;

Explanation:

The correct way to declare a single-dimensional array in C# is 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

Answer:

d) A framework for querying data in a consistent manner

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?

a) string str = null;
b) string str = “”;
c) string str = ” “;
d) string str = String.Empty;

Answer:

d) string str = String.Empty;

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?

a) Catches exceptions
b) Throws exceptions
c) Executes regardless of whether an exception is thrown or caught
d) Stops exceptions from propagating

Answer:

c) Executes regardless of whether an exception is thrown or caught

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?

a) const
b) readonly
c) final
d) static

Answer:

a) const

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#?

a) Length()
b) Size()
c) Count()
d) Length

Answer:

d) Length

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#?

a) Object
b) Base
c) Root
d) System

Answer:

a) Object

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#?

a) public
b) private
c) protected
d) secured

Answer:

d) secured

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?

a) int
b) char
c) bool
d) string

Answer:

d) string

Explanation:

string is the reference type in C#.

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

Answer:

a) Checks type compatibility

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?

a) final
b) static
c) sealed
d) abstract

Answer:

c) sealed

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#?

a) delegate void() { }
b) func void() { }
c) delegate() { }
d) delegate void { }

Answer:

c) delegate() { }

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#?

a) int? x;
b) nullable int x;
c) int x?;
d) int x = null;

Answer:

a) int? x;

Explanation:

In C#, the ? modifier is used to declare a nullable value type.

37. Which of the following is NOT a valid C# collection?

a) ArrayList
b) HashTable
c) LinkList
d) Queue

Answer:

c) LinkList

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#?

a) delete()
b) free()
c) remove()
d) Dispose()

Answer:

d) Dispose()

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#?

a) catch
b) throw
c) error
d) handle

Answer:

a) catch

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#?

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

Answer:

b) It checks for type compatibility and returns null if the conversion is not possible

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?

a) long
b) short
c) int
d) byte

Answer:

c) int

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#?

a) abstracted
b) abs
c) abstract
d) abstraction

Answer:

c) abstract

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?

a) Static method
b) Virtual method
c) Sealed method
d) Private method

Answer:

b) Virtual method

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#?

a) true
b) false
c) null
d) 0

Answer:

b) false

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?

a) List
b) Dictionary
c) Queue
d) HashSet

Answer:

d) HashSet

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?

a) is
b) typeof
c) sizeof
d) type

Answer:

b) typeof

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?

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.

Answer:

c) Delegates are type-safe.

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?

a) Overloading
b) Overriding
c) Hiding
d) Abstracting

Answer:

a) Overloading

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?

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.

Answer:

c) The method can be overridden in the derived class.

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?

a) int[] array = new int[5];
b) int[] array = 5;
c) int array[5];
d) int array = new int[5];

Answer:

a) int[] array = new int[5];

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.


Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top