My Report

C# Exception Handling Test – 2


Correct Answer: 2 points | Wrong: -1 point
Grades: A* (100% score) | A (80%-99%) | B (60%-80%) | C (40%-60%) | D (0%-40%)
advertisement

1. What will be the output of the following C# code snippet?

 {
     try 
     {
         int a, b;
         b = 0;
         a = 5 / b;
         Console.WriteLine("A");
     }
     catch(ArithmeticException e) 
     {
         Console.WriteLine("B");
     }
     finally
     {
         Console.WriteLine("C");
     }
     Console.ReadLine();
 }

2. Choose the correct statement which makes exception handling work in C#.NET?

3. What will be the output of the following C# code?

 {
     try 
     {
         int i, sum;
         sum = 10;
         for (i = -1 ;i < 3 ;++i) 
         {
             sum = (sum / i);
             Console.WriteLine(i);
         }
     }
     catch(ArithmeticException e) 
     {
         Console.WriteLine("0");
     }
     Console.ReadLine();
 }
 

4. What will be the output of the following C# code?

  {
      int sum = 10;
      try
      {
          int i;
          for (i = -1; i < 3; ++i)
          sum = (sum / i);
      }
      catch (ArithmeticException e)
      {
          Console.WriteLine("0");
      }
      Console.WriteLine(sum);
      Console.ReadLine();
  }
 

5. What will be the output of the following C# code?

{
    try 
    {
        int []a = {1, 2, 3, 4, 5};
        for (int i = 0; i < 5; ++i) 
        Console.WriteLine(a[i]);
        int x = (1 / Convert.ToInt32(0));
    }
    catch(IndexOutOfRangeException e) 
    {
        Console.WriteLine("A");        	
    }
    catch(ArithmeticException e) 
    {     	
        Console.WriteLine("B");
    }
    Console.ReadLine();
} 

6. What will be the output of the following C# code snippet?

 class Program
 {
     static void Main(string[] args)
     {
         int i;
         int v = 40;
         int[] x = new int[5];
         try
         {
             Console.WriteLine(" Enter the number: ");
             index = Convert.ToInt32(Console.ReadLine());
             x[index] = v;
         }
         catch(Exception e)
         {
             Console.WriteLine("Exception occurred");
         }
         Console.WriteLine("Program executed");
     }
 }

7. When is no exception thrown at runtime then who will catch it?

8. What will be the output of the following C# code?

{
    try 
    {
        int []a = {1, 2, 3, 4, 5};
        for (int i = 0; i < 7; ++i) 
        Console.WriteLine(a[i]);
    }
    catch(IndexOutOfRangeException e) 
    {
        Console.WriteLine("0");        	
    }
    Console.ReadLine();
}

9. What will be the output of the following C# code snippet?

 {
     try 
     {
         int a, b;
         b = 0;
         a = 10 / b;
         Console.WriteLine("A");
     }
     catch(ArithmeticException e) 
     {
         Console.WriteLine("B");        	
     }
     Console.ReadLine();
 }

10. What will be the output of the following C# code?

 public  static void Main(string[] args)
 {
     try
     {
         int a, b, c = 5;
         b = 0;
         a = c / b;
         Console.WriteLine("A");
     }
     catch (ArithmeticException e)
     {
         int c = 5;
         int i = 10;
         int z = 2 * c - i;
         Console.WriteLine("B");
         Console.WriteLine(z);
     }
     Console.ReadLine();
 }

 

Start practicing “1000 MCQs on C#”, and once you are ready, you can take tests on all topics by attempting our “C# Test Series”.

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