This set of C# Questions and Answers focuses on “Exceptions of Type Finally and Built in Exceptions”.
1. Which of these clauses will be executed even if no exceptions are found?
a) throws
b) finally
c) throw
d) catch
View Answer
Explanation: finally keyword is used to define a set of instructions that will be executed irrespective of whether the exception is found or not.
2. A single try block must be followed by which of these?
a) finally
b) catch
c) Both finally & catch
d) None of the mentioned
View Answer
Explanation: Try block can be followed by any of finally or catch block, try block checks for exceptions and work is performed by finally and catch block as per the exception.
3. Which of these exceptions handles the divide by zero error?
a) ArithmeticException
b) MathException
c) IllegalAccessException
d) IllegarException
View Answer
Explanation: None.
4. Which of these exceptions will occur if we try to access the index of an array beyond its length?
a) ArithmeticException
b) ArrayException
c) ArrayArguementException
d) IndexOutOfRangeException
View Answer
Explanation: IndexOutOfRangeException is a built in exception that is caused when we try to access an index location which is beyond the length of an array.
5. What will be the output of the following C# code snippet?
class program
{
public static void Main(string[] args)
{
try
{
int a = args.Length;
int b = 1 / a;
Console.WriteLine(a);
}
catch (ArithmeticException e)
{
Console.WriteLine("1");
}
Console.ReadLine();
}
}
a) 0
b) 1
c) Compile time error
d) Runtime error
View Answer
Explanation: None.
6. What will be the output of the following C# code snippet?
class program
{
public static void Main(string[] args)
{
try
{
throw new NullReferenceException("C");
Console.WriteLine("A");
}
catch (ArithmeticException e)
{
Console.WriteLine("B");
}
Console.ReadLine();
}
}
a) A
b) B
c) Compile time error
d) Runtime error
View Answer
Explanation: Try block is throwing NullPointerException but the catch block is used to counter Arithmetic Exception. Hence NullPointerException occurs since no catch is there which can handle it, runtime error occurs.
7. What will be the output of the following C# code snippet?
class Program
{
public static void Main(string[] args)
{
try
{
int a = 1;
int b = 10 / a;
try
{
if (a == 1)
a = a / a - a;
if (a == 2)
{
int[] c = { 1 };
c[8] = 9;
}
}
finally
{
Console.WriteLine("A");
}
}
catch (IndexOutOfRangeException e)
{
Console.WriteLine("B");
}
Console.ReadLine();
}
}
a) A
b) B
c) AB
d) BA
View Answer
Explanation: The inner try block does not have a catch which can tackle IndexOutOfRangeException hence finally is executed which prints ‘A’. The outer try block does have catch for IndexOutOfBoundException exception but no such exception occurs in it hence its catch is never executed and only ‘A’ is printed.
8. What will be the output of the following C# code snippet?
class Program
{
public static void Main(string[] args)
{
try
{
int a = args.Length;
int b = 10 / a;
Console.WriteLine(a);
try
{
if (a == 1)
a = a / a - a;
if (a == 2)
{
int[] c = { 1 };
c[8] = 9;
}
}
catch (IndexOutOfRangeException e)
{
Console.WriteLine("TypeA");
}
}
catch (ArithmeticException e)
{
Console.WriteLine("TypeB");
}
Console.ReadLine();
}
}
a) TypeA
b) TypeB
c) 0TypeA
d) Compile time error
View Answer
Explanation: None.
9. Which of the following keywords is used by the calling function to guard against the exception that is thrown by called function?
a) try
b) throw
c) throws
d) catch
View Answer
Explanation: If a method is capable of causing an exception that it does not handle. It must specify this behaviour so that callers of the method can guard themselves against that exception. This is done by using throws clause in methods declaration.
10. Which of these classes is related to all the exceptions that are explicitly thrown?
a) Error
b) Exception
c) Throwable
d) Throw
View Answer
Explanation: None.
Sanfoundry Global Education & Learning Series – C# Programming Language.
Here’s the list of Best Books in C# Programming Language.
To practice all areas of C# for Interviews, here is complete set on 1000+ Multiple Choice Questions and Answers on C#.
- Check C# Books
- Practice MCA MCQs
- Apply for C# Internship
- Apply for Computer Science Internship
- Check Computer Science Books