Vidyalelo
C# Programming · all questions

Exception Handling in C Sharp
practice.

Practice every MCQ with options. Use Show answers when you want the correct option and solution.

107

Questions

4/6

Page

Pick an option on a question to see the right answer and solution.

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();
}

Select an option to see the answer and solution.

Select the modifiers which can be used with the properties?

Select an option to see the answer and solution.

Which of the following is the wrong statement about exception handling in C#.NET?

Select an option to see the answer and solution.

Which of these keywords are used for the block to handle the exceptions generated by try block?

Select an option to see the answer and solution.

What will be the output of the following C# code?
class student
{
    int []scores = new int[5] {23, 42, 54, 11, 65};
    public int this[int index]
    {
        get
        {
            if (index < 5)
            return scores[index];
            else
            {
                Console.WriteLine("invalid index");
                return 0;
            }
        }
        set
        {
            if (index < 5)
            scores[index] = value;
            else
            Console.WriteLine("invalid index");
        }
    }
}
class Program
{
    public static void Main(string[] args)
    {
        student s = new student();
        Console.WriteLine(s[4] + 8);
        Console.ReadLine();
    }
}

Select an option to see the answer and solution.

Select the statements which describe the correct usage of exception handling over conventional error handling approaches?

Select an option to see the answer and solution.

Which of these exceptions will occur if we try to access the index of an array beyond its length?

Select an option to see the answer and solution.

Consider a class maths and we had a property called as sum.b which is the reference to a maths object and we want the statement Console.WriteLine(b.sum)to fail. Which among the following is the correct solution to ensure this functionality?

Select an option to see the answer and solution.

Consider a class maths and we had a property called as sum.b is a reference to a maths object and we want the code below to work. Which is the correct solution to ensure this functionality?
b.maths = 10;
Console.WriteLine(b.maths);

Select an option to see the answer and solution.

What will be the output of the following C# code?
class program
{
    static void main(string[] args)
    {
        int i = 5;
        int v = 40;
        int[] p = new int[4];
        try
        {
            p[i] = v;
        }
        catch(IndexOutOfRangeException e)
        {
            Console.WriteLine("Index out of bounds");
        }
        Console.WriteLine("Remaining program");
    }
}

Select an option to see the answer and solution.

If math class had add property with get and set accessors, then which of the following statements will work correctly?

Select an option to see the answer and solution.

Which of these clauses will be executed even if no exceptions are found?

Select an option to see the answer and solution.

Choose the statements which makes use of essential properties rather than making data member public in C#.NET?

Select an option to see the answer and solution.

What will be the output of the following C# code?
class Program
{
     static void Main(string[] args)
     {            
         try 
         {
             Console.WriteLine("csharp" + " " + 1/0);
         }
         finally
         {
             Console.WriteLine("Java");        	
         }
         Console.ReadLine();
     }
}

Select an option to see the answer and solution.

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();
}

Select an option to see the answer and solution.

What will be the output of the following C# code?
class number
{
    private int num1;
    private int num2;
    public int anumber
    {
        get
        {
            return num1;
        }
        set
        {
            num1 = value;
        }
    }
    public int anumber1
    {
        get
        {
            return num2;
        }
        set
        {
            num2 = value;
        }
    }
}
class Program
{
    public static void Main(string[] args)
    {
        number p = new number();
        p.anumber = 20;
        number k = new number();
        k.anumber1 = 40; 
        int m = p.anumber;
        int t = k.anumber1;
        int r = p.anumber + k.anumber1;
        System.Console.WriteLine("number1 = " +m);
        System.Console.WriteLine("number2 = " +t);
        System.Console.WriteLine("sum = " +r);
        System.Console.ReadLine();
    }
}

Select an option to see the answer and solution.

Choose the correct statement among the followings?

Select an option to see the answer and solution.

What will be the output of the following C# code?
class list
{
    ArrayList array = new ArrayList();
    public object this[int index]
    {
        get
        {
            if (index < 0 || index >= array.Count)
            {
                return null;
            }
            else
            {
                return (array[index]);
            }
        }
        set
        {
            array[index] = value;
        }
    }
    public int Count 
   { 
       get;
       set; 
   }
}
class Program
{
    static void Main(string[] args)
    {
        list list1 = new list();
        list1[0] = "123";
        list1[1] = " abc ";
        list1[2] = "xyz";
        for (int i = 0; i<=list1.Count; i++)
        Console.WriteLine(list1[i]);
        Console.ReadLine();
    }
}

Select an option to see the answer and solution.

If the math class had add property with get accessors then which of the following statements will work correctly?

Select an option to see the answer and solution.

What will be the output of the following C# code?
class number
{
    private int num1 = 60;
    private int num2 = 20;
    public int anumber
    {
        get
        {
            return num1;
        }
        set
        {
            num1 = value;
        }
    }
    public int anumber1
    {
        get
        {
            return num2;
        }
        set
        {
            num2 = value;
        }
    }
}
class Program
{
    public static void Main(string[] args)
    {
        number p = new number();
        number k = new number();
        int m = p.anumber;
        int t = k.anumber1;
        int r = p.anumber * k.anumber1;
        Console.WriteLine("sum = " + r);
        Console.ReadLine();
    }
}

Select an option to see the answer and solution.