C# Programming - Exception Handling - Discussion
Discussion Forum : 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");
}
}
}
Discussion:
6 comments Page 1 of 1.
Satham said:
1 decade ago
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.
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.
G.Durga said:
1 decade ago
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'.
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'.
Sujoy Mondal said:
1 decade ago
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.
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.
(1)
Jitendra patil said:
1 decade ago
As a first statement in catch block will be executed first then the last statement after catch block will definitely execute.
Ramakrishna said:
1 decade ago
Given all statements are in order. Whatever we given the console statements those will be executed definitely.
John said:
1 decade ago
Because it will be handled by default exception handler.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers