C# Programming - Exception Handling - Discussion

Discussion :: Exception Handling - General Questions (Q.No.4)

4. 

Which of the following statements is correct about the C#.NET program given below?

using System;
namespace IndiabixConsoleApplication
{
    class MyProgram
    {
        static void Main(string[] args)
        {
            int index = 6;
            int val = 44;
            int[] a = new int[5];
            try
            {
                a[index] = val ;
            }    
            catch(IndexOutOfRangeException e)
            {
                Console.Write("Index out of bounds ");
            }
            Console.Write("Remaining program");
        }
    }
}

[A]. Value 44 will get assigned to a[6].
[B]. It will output: Index out of bounds
[C]. It will output: Remaining program
[D]. It will not produce any output.
[E]. It will output: Index out of bounds Remaining program

Answer: Option E

Explanation:

No answer description available for this question.

Jitendra Patil said: (Jul 13, 2012)  
As a first statement in catch block will be executed first then the last statement after catch block will definitely execute.

Ramakrishna said: (Dec 15, 2012)  
Given all statements are in order. Whatever we given the console statements those will be executed definitely.

John said: (Jan 11, 2013)  
Because it will be handled by default exception handler.

G.Durga said: (Jun 17, 2013)  
Generally in arrays index starts from '0'.

Here we declared an array named a with size 5.

So it have the capacity to contain only 6 elements.

So it doesn't take in a[6] value, but we are passing value 6, so the control goes to catch block. Then remaining statements will be executed. Finally the result come as 'index out of bounds Remaining Program'.

Satham said: (Dec 2, 2013)  
Arrays are zero based index types.

So that array[5] will have the index as [0][1][2][3][4][5] only.

But if we assign a[6] as the index, it will exceed the actual array index (5). So that catch block will be executed and then the following statement is also be executed.

So that the result would be "Index out of bounds Remaining program" only.

Sujoy Mondal said: (May 20, 2015)  
Array Index a[] = [0, 1, 2, 3, 4, 5].

But Index is 6. So a[index] = val.

=> a[6] = 44.

Index crossed the define array max index (define array index max = 5).

So result Index out of bounds.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.