C# Questions & Answers – Exceptions of Type Finally and Built in Exceptions

This set of C# Questions and Answers focuses 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
View Answer

Answer: b
Explanation: 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
View Answer

Answer: c
Explanation: 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
View Answer

Answer: a
Explanation: None.
advertisement
advertisement

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

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

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
  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
View Answer

Answer: b
Explanation: None.
advertisement

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

Answer: d
Explanation: 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.
advertisement

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

Answer: a
Explanation: 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
View Answer

Answer: b
Explanation: 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
View Answer

Answer: c
Explanation: 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
View Answer

Answer: c
Explanation: None.

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

Here’s the list of Best Books in C# Programming Language.

To practice all areas of C# for Interviews, here is complete set on 1000+ Multiple Choice Questions and Answers on C#.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.