C#Questions & Answers – Try & Catch in Detail

«
»

This section of our 1000+ C# MCQs focuses 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
View Answer

Answer: d
Explanation: None.
advertisement

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

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

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

Answer: d
Explanation: try followed by either catch or finally block.
Participate in C# Programming Certification Contest of the Month Now!
advertisement

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

Answer: b
Explanation: None.
advertisement

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

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

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

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

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

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

Answer: a
Explanation: None.

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

Answer: c
Explanation: None.

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

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

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 & technical discussions at Telegram SanfoundryClasses.