C# Questions & Answers on Implementation of Exception Handling for Freshers

https://www.computersprofessor.com/2018/07/c-questions-answers-on-implementation.html
1. What will be the output of the following set of 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
b) 0 5
c) 0 -10
d) Compile time error
Answer: c
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 set of 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
b) 12345
c) Run time error
d) 12345B
Answer: d
Explanation: Due to occurrence of arithmetic exception here ‘B’ is printed after 12345.
Output : 12345B
3. What will be the output of given code snippet?
{
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
b) 123450
c) 1234500
d) Compile time error
Answer: b
Explanation: When array index goes out of bound then IndexOutOfBoundsException exception is thrown by the system.
Output : 123450
4. What would be the output of following 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
b) B
c) Compile time error
d) Run time error
Answer: b
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 would be the output of following code snippet?
{
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
b) 0
c) -1 0
d) -1 0 -1
Answer: c
Output : -1 0
6. What would be the output of following 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
b) B
c) B C
d) Run time error
Answer: c
Explanation: finally keyword is used to execute before catch and try block is executed.
Output : B C
7. What would be the output of given code snippet?
class Program
{
static void Main(string[] args)
{
int i;
int v = 40;
int[] x = new int[5];
try
{
Console.WriteLine(" Enter the number: ");
index = Convert.ToInt32(Console.ReadLine());
x[index] = v;
}
catch(Exception e)
{
Console.WriteLine("Exception occured");
}
Console.WriteLine("Program executed");
}
}
a) Exception occured
b) Program executed
c) Exception occured
Program executed
d) Program executed
Exception occured
b) Program executed
c) Exception occured
Program executed
d) Program executed
Exception occured
Answer: c
Output : Exception occured
Program executed
Program executed
8. When no exception is thrown at runtime then who will catch it?
a) CLR
b) Operating System
c) Loader
d) Compiler
Answer: a
9. What would be the output of given code snippet?
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
b) Run time error
c) B 0
d) B
Answer: c
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
Answer: d
Explanation: By definition of exceptionhandling mechanism in C#.NET.