What is the output of the following?
class GFG
{
public static void main (String[] args)
{
int[] arr = new int[2];
arr[0] = 10;
arr[1] = 20;
for (int i = 0; i <= arr.length; i++)
System.out.println(arr[i]);
}
}
out put of function
Answers
Answered by
18
Answer:
Array out of bound is the answer for this program
Answered by
5
In the output of the following program, we will get an error message "array out of bond".
In the given program the user is trying to print the values inside an array using a for-loop.
First, he created a new array of length two units and then assigned values to both the places inside the array.
Now, we know that the two places inside the array will be sequenced as 0 and 1.
So when in the for-loop the user is initializing the value of 'i' from 0 and going up to 'i <= arr.length' here in this step the value i = 2 will also be true according to the given condition but there is no pace for i = 2 inside the array, therefore, an error message of "array out of bond" will be displayed.
Similar questions
History,
2 months ago
Biology,
4 months ago
Computer Science,
9 months ago
India Languages,
9 months ago