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

Java Multiple Choice Questions & Answers on Creating Threads for Freshers

1. Which of these keywords are used to implement synchronization? a) sunchronizeb) sync) synchd) synchronized Answer: d 2. Which of these method is used to avoid polling in Java? a) wait()b) not...

C Programming Multiple Choice Questions and Answers on Error Handling in File for Freshers

1. What is the output of this C code if there is no error in stream fp? #include int main() { FILE *fp; fp = fopen("newfile", "w"); printf("%d\n", f...

C# Multiple Choice Questions & Answers on Initialization of Variables for Freshers

1. Select output for the following set of code. static void Main(string[] args) { int a = 5; int b = 10; int c; Console.WriteLine(c = ++ a + b ++); Console...

Post a Comment

emo-but-icon
:noprob:
:smile:
:shy:
:trope:
:sneered:
:happy:
:escort:
:rapt:
:love:
:heart:
:angry:
:hate:
:sad:
:sigh:
:disappointed:
:cry:
:fear:
:surprise:
:unbelieve:
:shit:
:like:
:dislike:
:clap:
:cuff:
:fist:
:ok:
:file:
:link:
:place:
:contact:

item