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

1. What will be the output of the following set of code?
  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
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?
  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
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?
  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
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?
  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
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?
  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
Answer: c
Output : -1 0
6. What would be the output of following 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
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?
  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 occured");
  17.          }
  18.          Console.WriteLine("Program executed");
  19.      }
  20.  }
a) Exception occured
b) Program executed
c) Exception occured
Program executed
d) Program executed
Exception occured
Answer: c

Output : Exception occured
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?
  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
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.

Related

Java Multiple Choice Questions & Answers on Java.util package Maps for Freshers

1. Which of these object stores association between keys and values? a) Hash tableb) Mapc) Arrayd) String Answer: b 2. Which of these classes provide implementation of map interface? a) ArrayLis...

C Programming Questions and Answers on Initialization of Pointer Arrays for Freshers

1. To declare a 3 dimension array using pointers, which of the following is the correct syntax: a) char *a[][];b) char **a[];c) char ***a;d) all of the mentioned Answer: a 2. Comment on the outpu...

Java Multiple Choice Questions & Answers on Java.util – LinkedList, HashSet & TreeSet Class for Freshers

1. Which of these standard collection classes implements a linked list data structure? a) AbstractListb) LinkedListc) HashSetd) AbstractSet Answer: b 2. Which of these classes implements Set inte...

Post a Comment

emo-but-icon
:noprob:
:smile:
:shy:
:trope:
:sneered:
:happy:
:escort:
:rapt:
:love:
:heart:
:angry:
:hate:
:sad:
:sigh:
:disappointed:
:cry:
:fear:
:surprise:
:unbelieve:
:shit:
:like:
:dislike:
:clap:
:cuff:
:fist:
:ok:
:file:
:link:
:place:
:contact:

item