C# banner

C# Multiple Choice Questions (MCQs) and Answers

Master C# with Practice MCQs. Explore our curated collection of Multiple Choice Questions. Ideal for placement and interview preparation, our questions range from basic to advanced, ensuring comprehensive coverage of C# concepts. Begin your placement preparation journey now!

Q121

Q121 What is the purpose of exception handling in C#?

A

To handle errors and exceptional conditions

B

To define variables

C

To create classes

D

To manage memory

Q122

Q122 Which keyword is used to catch exceptions in C#?

A

try

B

catch

C

throw

D

finally

Q123

Q123 What does the 'finally' block do in exception handling?

A

Executes only if an exception is thrown

B

Executes regardless of an exception

C

Executes before the catch block

D

Executes only if no exception is thrown

Q124

Q124 What is the difference between 'throw' and 'throw ex' in C#?

A

'throw' rethrows the original exception, 'throw ex' throws a new exception

B

There is no difference

C

'throw' is used for system exceptions, 'throw ex' is used for application exceptions

D

'throw' stops the program, 'throw ex' continues the program

Q125

Q125 How do you write a try-catch block in C#?

A

try { // code } catch (Exception e) { // code }

B

try { // code } catch { // code }

C

try catch { // code }

D

try { // code } finally { // code }

Q126

Q126 How do you throw an exception in C#?

A

throw new Exception("Error message");

B

throw Exception("Error message");

C

throw "Error message";

D

throw Exception;

Q127

Q127 You get an 'Unhandled Exception' error.
What is the likely cause?

A

The exception is not caught by any catch block

B

The exception is caught by the catch block

C

The finally block is missing

D

The try block is empty

Q128

Q128 You encounter a 'stack overflow' exception.
What could be the cause?

A

Infinite recursion

B

Division by zero

C

Accessing null object

D

Array index out of bounds

Q129

Q129 You get a 'NullReferenceException'.
What is the likely cause?

A

Accessing a method on a null object

B

Array index out of bounds

C

Division by zero

D

Infinite loop

Q130

Q130 What is a delegate in C#?

A

A type-safe function pointer

B

A class

C

An interface

D

A struct

Q131

Q131 What is the main use of delegates in C#?

A

To handle exceptions

B

To allow methods to be passed as parameters

C

To define classes

D

To manage memory

Q132

Q132 What is an event in C#?

A

A method

B

A delegate that is associated with a specific action

C

A class

D

A type of loop

Q133

Q133 How do you declare a delegate in C#?

A

delegate void MyDelegate(int x);

B

void delegate MyDelegate(int x);

C

delegate MyDelegate void(int x);

D

void MyDelegate delegate(int x);

Q134

Q134 How do you invoke a delegate in C#?

A

myDelegate.Invoke(10);

B

Invoke(myDelegate, 10);

C

call myDelegate(10);

D

execute myDelegate(10);

Q135

Q135 You get an 'object not set to an instance of an object' error when using a delegate.
What is the likely cause?

A

Delegate is null

B

Delegate is already invoked

C

Delegate is not declared

D

Delegate is not assigned to an event

Q136

Q136 You encounter an 'event handler not found' error.
What could be the cause?

A

The event is not defined

B

The event handler method signature does not match the delegate

C

The delegate is not instantiated

D

The event is private

Q137

Q137 What is LINQ in C#?

A

A method for asynchronous programming

B

A type-safe function pointer

C

A query syntax to retrieve data from different sources

D

A way to handle exceptions

Q138

Q138 What does the 'Select' clause in LINQ do?

A

Filters elements based on a condition

B

Projects each element into a new form

C

Sorts the elements

D

Groups the elements

Q139

Q139 What is a lambda expression in C#?

A

A way to define a delegate inline

B

A type of loop

C

A method to handle exceptions

D

A way to declare a class

Q140

Q140 How do you write a lambda expression to add two numbers in C#?

A

(a, b) => a + b

B

(a, b) -> a + b

C

(a, b) { a + b; }

D

(a, b) +> a + b

Q141

Q141 How do you use LINQ to find the sum of all elements in a list of integers in C#?

A

list.Sum();

B

list.Aggregate();

C

list.Count();

D

list.Sum((x) => x);

Q142

Q142 You get an 'InvalidOperationException' when using LINQ.
What is the likely cause?

A

The query is empty

B

The query has syntax errors

C

The data source is null

D

LINQ is not supported

Q143

Q143 You encounter a 'null reference' exception when using a lambda expression.
What could be the cause?

A

The lambda expression is not assigned

B

The data source is null

C

The lambda expression is incorrect

D

The lambda expression is not defined

Q144

Q144 What is the purpose of the 'FileStream' class in C#?

A

To handle memory management

B

To manage database connections

C

To read from and write to files

D

To handle network communications

Q145

Q145 What is the difference between 'StreamReader' and 'StreamWriter' in C#?

A

'StreamReader' is for reading files, 'StreamWriter' is for writing files

B

'StreamReader' is for binary files, 'StreamWriter' is for text files

C

'StreamReader' is for small files, 'StreamWriter' is for large files

D

There is no difference

Q146

Q146 What is the function of the 'File' class in C#?

A

Provides static methods for file operations

B

Manages database connections

C

Handles network communications

D

Manages memory allocation

Q147

Q147 How do you create a new file in C# using the 'File' class?

A

File.Create("path");

B

File.New("path");

C

File.Make("path");

D

File.Build("path");

Q148

Q148 How do you read all lines of a text file into a string array in C#?

A

File.ReadAllLines("path");

B

File.ReadAllText("path");

C

File.LoadLines("path");

D

File.LoadText("path");

Q149

Q149 You get an 'UnauthorizedAccessException' when trying to access a file.
What is the likely cause?

A

The file is read-only

B

The file does not exist

C

The file is open in another application

D

The file path is incorrect

Q150

Q150 You encounter a 'FileNotFoundException'.
What could be the cause?

A

The file path is incorrect

B

The file is corrupted

C

The file is locked

D

The file is read-only

ad verticalad vertical
ad