This section of our 1000+ C# multiple choice questions focuses on implementation of the exception handling in C# Programming Language.
1. What will be the output of the following C# code?
{
int sum = 10;
try
{
int i;
for (i = -1; i < 3; ++i)
sum = (sum / i);
}
catch (ArithmeticException e)
{
Console.WriteLine("0");
}
Console.WriteLine(sum);
Console.ReadLine();
}
a) 0
b) 0 5
c) 0 -10
d) Compile time error
View Answer
Explanation: Value of variable sum is printed as sum and is defined outside try & catch block. If defined inside the try block then sum would be undefined for execution.
Output : 0 -10
2. What will be the output of the following C# code?
{
try
{
int []a = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; ++i)
Console.WriteLine(a[i]);
int x = (1 / Convert.ToInt32(0));
}
catch(IndexOutOfRangeException e)
{
Console.WriteLine("A");
}
catch(ArithmeticException e)
{
Console.WriteLine("B");
}
Console.ReadLine();
}
a) 1234
b) 12345
c) Run time error
d) 12345B
View Answer
Explanation: Due to occurrence of arithmetic exception here ‘B’ is printed after 12345.
Output : 12345B
3. What will be the output of the following C# code?
{
try
{
int []a = {1, 2, 3, 4, 5};
for (int i = 0; i < 7; ++i)
Console.WriteLine(a[i]);
}
catch(IndexOutOfRangeException e)
{
Console.WriteLine("0");
}
Console.ReadLine();
}
a) 12345
b) 123450
c) 1234500
d) Compile time error
View Answer
Explanation: When array index goes out of bound then IndexOutOfBoundsException exception is thrown by the system.
Output : 123450
4. What will be the output of the following C# code snippet?
{
try
{
int a, b;
b = 0;
a = 10 / b;
Console.WriteLine("A");
}
catch(ArithmeticException e)
{
Console.WriteLine("B");
}
Console.ReadLine();
}
a) A
b) B
c) Compile time error
d) Run time error
View Answer
Explanation: Since b = 0 since a = 10 / 0 so, arithmetic exception is caught and hence statement in catch block is executed.
Output : B
5. What will be the output of the following C# code?
{
try
{
int i, sum;
sum = 10;
for (i = -1 ;i < 3 ;++i)
{
sum = (sum / i);
Console.WriteLine(i);
}
}
catch(ArithmeticException e)
{
Console.WriteLine("0");
}
Console.ReadLine();
}
a) -1
b) 0
c) -1 0
d) -1 0 -1
View Answer
Explanation: None.
Output : -1 0
6. What will be the output of the following C# code snippet?
{
try
{
int a, b;
b = 0;
a = 5 / b;
Console.WriteLine("A");
}
catch(ArithmeticException e)
{
Console.WriteLine("B");
}
finally
{
Console.WriteLine("C");
}
Console.ReadLine();
}
a) A
b) B
c) B C
d) Run time error
View Answer
Explanation: finally keyword is used to execute before catch and try block is executed.
Output : B C
7. What will be the output of the following C# code snippet?
class Program
{
static void Main(string[] args)
{
int i;
int v = 40;
int[] x = new int[5];
int index;
try
{
Console.WriteLine(" Enter the number: ");
index = Convert.ToInt32(Console.ReadLine());
x[index] = v;
}
catch(Exception e)
{
Console.WriteLine("Exception occurred");
}
Console.WriteLine("Program executed");
}
}
a) Exception occurred
b) Program executed
c)
Exception occurred Program executed
d)
Program executed Exception occurredView Answer
Explanation: None.
Output: Exception occurred
Program executed
8. When is no exception thrown at runtime then who will catch it?
a) CLR
b) Operating System
c) Loader
d) Compiler
View Answer
Explanation: None.
9. What will be the output of the following C# code?
public static void Main(string[] args)
{
try
{
int a, b, c = 5;
b = 0;
a = c / b;
Console.WriteLine("A");
}
catch (ArithmeticException e)
{
int c = 5;
int i = 10;
int z = 2 * c - i;
Console.WriteLine("B");
Console.WriteLine(z);
}
Console.ReadLine();
}
a) Compile time error
b) Run time error
c) B 0
d) B
View Answer
Explanation: The catch block is called, as the exception is caught by the same block and hence statements are executed consecutively.
Output : B 0
10. Choose the correct statement which makes exception handling work in C#.NET?
a) .Net runtime makes search for the exception handler where exception occurs
b) If no exception is matched, exception handler goes up the stack and hence finds the match there
c) If no match is found at the highest level of stack call, then unhandledException is generated and hence termination of program occurs
d) All of the mentioned
View Answer
Explanation: By definition of exceptionhandling mechanism in C#.NET.
Sanfoundry Global Education & Learning Series – C# Programming Language.
To practice all areas of C# language, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Check MCA Books
- Apply for C# Internship
- Apply for Computer Science Internship
- Practice Computer Science MCQs
- Check C# Books