C# Questions & Answers – Implementation of Exception Handling

«
»

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?

advertisement
  1.   {
  2.       int sum = 10;
  3.       try
  4.       {
  5.           int i;
  6.           for (i = -1; i < 3; ++i)
  7.           sum = (sum / i);
  8.       }
  9.       catch (ArithmeticException e)
  10.       {
  11.           Console.WriteLine("0");
  12.       }
  13.       Console.WriteLine(sum);
  14.       Console.ReadLine();
  15.   }

a) 0
b) 0 5
c) 0 -10
d) Compile time error
View Answer

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

  1. {
  2.     try 
  3.     {
  4.         int []a = {1, 2, 3, 4, 5};
  5.         for (int i = 0; i < 5; ++i) 
  6.         Console.WriteLine(a[i]);
  7.         int x = (1 / Convert.ToInt32(0));
  8.     }
  9.     catch(IndexOutOfRangeException e) 
  10.     {
  11.         Console.WriteLine("A");        	
  12.     }
  13.     catch(ArithmeticException e) 
  14.     {     	
  15.         Console.WriteLine("B");
  16.     }
  17.     Console.ReadLine();
  18. }

a) 1234
b) 12345
c) Run time error
d) 12345B
View Answer

Answer: d
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?

  1. {
  2.     try 
  3.     {
  4.         int []a = {1, 2, 3, 4, 5};
  5.         for (int i = 0; i < 7; ++i) 
  6.         Console.WriteLine(a[i]);
  7.     }
  8.     catch(IndexOutOfRangeException e) 
  9.     {
  10.         Console.WriteLine("0");        	
  11.     }
  12.     Console.ReadLine();
  13. }

a) 12345
b) 123450
c) 1234500
d) Compile time error
View Answer

Answer: b
Explanation: When array index goes out of bound then IndexOutOfBoundsException exception is thrown by the system.
Output : 123450
advertisement

4. What will be the output of the following C# code snippet?

  1.  {
  2.      try 
  3.      {
  4.          int a, b;
  5.          b = 0;
  6.          a = 10 / b;
  7.          Console.WriteLine("A");
  8.      }
  9.      catch(ArithmeticException e) 
  10.      {
  11.          Console.WriteLine("B");        	
  12.      }
  13.      Console.ReadLine();
  14.  }

a) A
b) B
c) Compile time error
d) Run time error
View Answer

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 will be the output of the following C# code?

  1.  {
  2.      try 
  3.      {
  4.          int i, sum;
  5.          sum = 10;
  6.          for (i = -1 ;i < 3 ;++i) 
  7.          {
  8.              sum = (sum / i);
  9.              Console.WriteLine(i);
  10.          }
  11.      }
  12.      catch(ArithmeticException e) 
  13.      {
  14.          Console.WriteLine("0");
  15.      }
  16.      Console.ReadLine();
  17.  }

a) -1
b) 0
c) -1 0
d) -1 0 -1
View Answer

Answer: c
Explanation: None.
Output : -1 0

6. What will be the output of the following C# code snippet?

  1.  {
  2.      try 
  3.      {
  4.          int a, b;
  5.          b = 0;
  6.          a = 5 / b;
  7.          Console.WriteLine("A");
  8.      }
  9.      catch(ArithmeticException e) 
  10.      {
  11.          Console.WriteLine("B");
  12.      }
  13.      finally
  14.      {
  15.          Console.WriteLine("C");
  16.      }
  17.      Console.ReadLine();
  18.  }

a) A
b) B
c) B C
d) Run time error
View Answer

Answer: c
Explanation: finally keyword is used to execute before catch and try block is executed.
Output : B C
advertisement

7. What will be the output of the following C# code snippet?

  1.  class Program
  2.  {
  3.      static void Main(string[] args)
  4.      {
  5.          int i;
  6.          int v = 40;
  7.          int[] x = new int[5];
  8.          try
  9.          {
  10.              Console.WriteLine(" Enter the number: ");
  11.              index = Convert.ToInt32(Console.ReadLine());
  12.              x[index] = v;
  13.          }
  14.          catch(Exception e)
  15.          {
  16.              Console.WriteLine("Exception occurred");
  17.          }
  18.          Console.WriteLine("Program executed");
  19.      }
  20.  }

a) Exception occurred
b) Program executed
c)

Exception occurred
Program executed

d)

Program executed
Exception occurred
View Answer
Answer: c
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

Answer: a
Explanation: None.

9. What will be the output of the following C# code?

  1.  public  static void Main(string[] args)
  2.  {
  3.      try
  4.      {
  5.          int a, b, c = 5;
  6.          b = 0;
  7.          a = c / b;
  8.          Console.WriteLine("A");
  9.      }
  10.      catch (ArithmeticException e)
  11.      {
  12.          int c = 5;
  13.          int i = 10;
  14.          int z = 2 * c - i;
  15.          Console.WriteLine("B");
  16.          Console.WriteLine(z);
  17.      }
  18.      Console.ReadLine();
  19.  }

a) Compile time error
b) Run time error
c) B 0
d) B
View Answer

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
View Answer

Answer: d
Explanation: By definition of exceptionhandling mechanism in C#.NET.

Sanfoundry Global Education & Learning Series – C# Programming Language.

advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn