250+ TOP MCQs on Exceptions of Type Finally and Built in Exceptions and Answers

This set of C# Interview Questions and Answers for Experienced people on “Exceptions of Type Finally and Built in Exceptions”.

1. Which of these clauses will be executed even if no exceptions are found?
a) throws
b) finally
c) throw
d) catch

Answer: b
Clarification: finally keyword is used to define a set of instructions that will be executed irrespective of whether the exception is found or not.

2. A single try block must be followed by which of these?
a) finally
b) catch
c) Both finally & catch
d) None of the mentioned

Answer: c
Clarification: Try block can be followed by any of finally or catch block, try block checks for exceptions and work is performed by finally and catch block as per the exception.

3. Which of these exceptions handles the divide by zero error?
a) ArithmeticException
b) MathException
c) IllegalAccessException
d) IllegarException

Answer: a
Clarification: None.

4. Which of these exceptions will occur if we try to access the index of an array beyond its length?
a) ArithmeticException
b) ArrayException
c) ArrayArguementException
d) IndexOutOfRangeException

Answer: d
Clarification: IndexOutOfRangeException is a built in exception that is caused when we try to access an index location which is beyond the length of an array.

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

  1.    class program
  2.    {
  3.        public static void Main(string[] args)
  4.        {
  5.            try
  6.            {
  7.                int a = args.Length;
  8.                int b = 1 / a;
  9.                Console.WriteLine(a);
  10.            }
  11.            catch (ArithmeticException e)
  12.            {
  13.                Console.WriteLine("1");
  14.            }
  15.            Console.ReadLine();
  16.        }
  17.    }

a) 0
b) 1
c) Compile time error
d) Runtime error

Answer: b
Clarification: None.

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

  1.  class program
  2.  {
  3.      public static void Main(string[] args)
  4.      {
  5.          try
  6.          {
  7.              throw new NullReferenceException("C");
  8.              Console.WriteLine("A");
  9.          }
  10.          catch (ArithmeticException e)
  11.          {
  12.              Console.WriteLine("B");
  13.          }
  14.          Console.ReadLine();
  15.      }
  16.  }

a) A
b) B
c) Compile time error
d) Runtime error

Answer: d
Clarification: Try block is throwing NullPointerException but the catch block is used to counter Arithmetic Exception. Hence NullPointerException occurs since no catch is there which can handle it, runtime error occurs.

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

  1.  class Program
  2.  {
  3.      public static void Main(string[] args)
  4.      {
  5.          try
  6.          {
  7.              int a = 1;
  8.              int b = 10 / a;
  9.              try
  10.              {
  11.                  if (a == 1)
  12.                      a = a / a - a;
  13.                  if (a == 2)
  14.                  {
  15.                      int[] c = { 1 };
  16.                      c[8] = 9;
  17.                  }
  18.              }
  19.              finally
  20.              {
  21.                  Console.WriteLine("A");
  22.              }
  23.         }
  24.         catch (IndexOutOfRangeException e)
  25.         {
  26.              Console.WriteLine("B");
  27.         }
  28.         Console.ReadLine();
  29.     }
  30.  }

a) A
b) B
c) AB
d) BA

Answer: a
Clarification: The inner try block does not have a catch which can tackle IndexOutOfRangeException hence finally is executed which prints ‘A’. The outer try block does have catch for IndexOutOfBoundException exception but no such exception occurs in it hence its catch is never executed and only ‘A’ is printed.

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

  1.  class Program
  2.  {
  3.      public static void Main(string[] args)
  4.      {
  5.          try
  6.          {
  7.              int a = args.Length;
  8.              int b = 10 / a;
  9.              Console.WriteLine(a);
  10.              try
  11.              {
  12.                  if (a == 1)
  13.                  a = a / a - a;
  14.                  if (a == 2)
  15.                  {
  16.                      int[] c = { 1 };
  17.                      c[8] = 9;
  18.                  }
  19.              }
  20.              catch (IndexOutOfRangeException e)
  21.              {
  22.                  Console.WriteLine("TypeA");
  23.              }
  24.          }
  25.          catch (ArithmeticException e)
  26.          {
  27.              Console.WriteLine("TypeB");
  28.          }
  29.          Console.ReadLine();
  30.      }
  31.  }

a) TypeA
b) TypeB
c) 0TypeA
d) Compile time error

Answer: b
Clarification: None.

9. Which of the following keywords is used by the calling function to guard against the exception that is thrown by called function?
a) try
b) throw
c) throws
d) catch

Answer: c
Clarification: If a method is capable of causing an exception that it does not handle. It must specify this behaviour so that callers of the method can guard themselves against that exception. This is done by using throws clause in methods declaration.

10. Which of these classes is related to all the exceptions that are explicitly thrown?
a) Error
b) Exception
c) Throwable
d) Throw

Answer: c
Clarification: None.

 

C# for Interviews, .

Leave a Reply

Your email address will not be published. Required fields are marked *