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
Explanation: None.
2. What will be the output of the following C# code?
class Output
{
public static void main(String args[])
{
try
{
int a = 9;
int b = 5;
int c = a / b - 5;
Console.WriteLine("Hello");
}
catch(Exception e)
{
Console.WriteLine("C");
}
finally
{
Console.WriteLine("sharp");
}
}
}
a) Hello
b) C
c) Hellosharp
d) Csharp
View Answer
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
View Answer
Explanation: try followed by either catch or finally block.
4. What will be the output of the following C# code?
class Output
{
public static void main(String args[])
{
try
{
int a = 10;
int b = 5;
int c = a / b - 5;
Console.WriteLine("Hi");
}
catch(Exception e)
{
Console.WriteLine("hello");
}
}
}
a) Hi
b) hello
c) Hihello
d) Compile time error
View Answer
Explanation: None.
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
Explanation: 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
View Answer
Explanation: None.
7. What will be the output of the following C# code?
class Output
{
public static void main(String args[])
{
try
{
int a = 5;
int b = 10;
int c = b / a - 5;
Console.WriteLine("Csharp");
}
}
}
a) Csharp
b) sharp
c) C
d) Compile time error
View Answer
Explanation: try should be followed by either catch or finally.
8. What will be the output of the following C# code snippet?
class Output
{
public static void main(String args[])
{
try
{
int a = 0;
int b = 5;
int c = a / b - 5;
Console.WriteLine("C");
}
finally
{
Console.WriteLine("sharp");
}
}
}
a) C
b) sharp
c) Csharp
d) None of the mentioned
View Answer
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?
class Output
{
public static void main(String args[])
{
try
{
int a = 10;
int b = 5;
int c = b - 5 / 5;
Console.WriteLine("Hi");
}
catch(Exception e)
{
Console.WriteLine("hello");
}
}
}
a) Hi
b) hello
c) Hihello
d) Compile time error
View Answer
Explanation: None.
10. Which of these keywords are used for generating an exception manually?
a) try
b) catch
c) throw
d) check
View Answer
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.
- Apply for C# Internship
- Check Computer Science Books
- Practice Computer Science MCQs
- Check MCA Books
- Check C# Books