C#Questions & Answers on Try & Catch in Detail for Freshers

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
2. What is the output of this program?
  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

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.
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

Explanation: try followed by either catch or finally block.
4. What will be the output of the program?
  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
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

Explanation: try is used for the block that needs to be checked for 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
7. What is the output of this program?
  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

Explanation: try should be followed by either catch or finally.
8. What is the output of the given 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

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 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
10. Which of these keywords are used for generating an exception manually?

a) try
b) catch
c) throw
d) check
Answer: c

Related

Multiple Choice Questions 3202731525633002566

Post a Comment

emo-but-icon

item