250+ TOP MCQs on Try & Catch in Detail and Answers

C# MCQs in detail on try & catch in C# Programming Language.

1. What is the use of try & catch?
a) It is used to manually handle the exception
b) It helps to fix the errors
c) It prevents automatic terminating of the program in cases when an exception occurs
d) All of the mentioned

Answer: d
Clarification: None.

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

  1.  class Output
  2.  {
  3.      public static void main(String args[])
  4.      {
  5. 
    
  6.          try
  7.          {
  8.              int a = 9;
  9.              int b = 5;
  10.              int c = a / b - 5;
  11.              Console.WriteLine("Hello");
  12.          }
  13.          catch(Exception e)
  14.          {
  15.              Console.WriteLine("C");
  16.          }
  17.          finally
  18.          {
  19.              Console.WriteLine("sharp");
  20.          }
  21.      }
  22.  }

a) Hello
b) C
c) Hellosharp
d) Csharp

Answer: d
Clarification: finally block execution takes place after the tryblock, no matter exception is found or not. catch block is executed only when exception is found. Here divide by zero exception is found hence both catch and finally are executed.

3. Choose the statement which is incorrect?
a) try block does not need to be followed by catch block
b) try block can be followed by finally block instead of catch block
c) try can be followed by both catch and finally block
d) try need not to be followed by anything

Answer: d
Clarification: try followed by either catch or finally block.

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

  1.   class Output
  2.   {
  3.       public static void main(String args[])
  4.       {
  5.           try
  6.           {
  7.               int a = 10;
  8.               int b = 5;
  9.               int c = a / b - 5;
  10.               Console.WriteLine("Hi");
  11.           }
  12.           catch(Exception e)
  13.           {
  14.               Console.WriteLine("hello");
  15.           }
  16.       }
  17.   }

a) Hi
b) hello
c) Hihello
d) Compile time error

Answer: b
Clarification: None.

5. Which of the keywords are used for the block to be examined for exceptions?
a) try
b) catch
c) throw
d) check

Answer: a
Clarification: try is used for the block that needs to be checked for the exception.

6. Which of these keywords are used for the block to handle the exceptions generated by try block?
a) try
b) catch
c) throw
d) check

Answer: b
Clarification: None.

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

  1.   class Output
  2.   {
  3.       public static void main(String args[])
  4.       {
  5.          try
  6.          {
  7.              int a = 5;
  8.              int b = 10;
  9.              int c = b / a - 5;
  10.              Console.WriteLine("Csharp");
  11.          }
  12.      }
  13.  }

a) Csharp
b) sharp
c) C
d) Compile time error

Answer: d
Clarification: try should be followed by either catch or finally.

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

  1.  class Output
  2.  {
  3.      public static void main(String args[])
  4.     {
  5.         try
  6.         {
  7.             int a = 0;
  8.             int b = 5;
  9.             int c = a / b - 5;
  10.             Console.WriteLine("C");
  11.         }
  12.         finally
  13.         {
  14.             Console.WriteLine("sharp");
  15.         }
  16.     }
  17. }

a) C
b) sharp
c) Csharp
d) None of the mentioned

Answer: c
Clarification: finally block is always executed after try block, no matter if the exception is found or not.

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

  1.  class Output
  2.  {
  3.      public static void main(String args[])
  4.      {
  5.          try
  6.          {
  7.              int a = 10;
  8.              int b = 5;
  9.              int c =  b - 5 / 5;
  10.              Console.WriteLine("Hi");
  11.          }
  12.          catch(Exception e)
  13.          {
  14.              Console.WriteLine("hello");
  15.          }
  16.      }
  17.  }

a) Hi
b) hello
c) Hihello
d) Compile time error

Answer: a
Clarification: None.

10. Which of these keywords are used for generating an exception manually?
a) try
b) catch
c) throw
d) check

Answer: c
Clarification: None.

Leave a Reply

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