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]);
}
}
Answers
Answer:
10 20 is the output of the given java program.but there is some error in this program ,so when you run it , it will show some error
Explanation:
in the print statement they called the array arr[i] which has the values 10,20 . so the op is 10 20
Answer: ArrayIndexOutOfBounds exception will occur.
Explanation:
The ArrayIndexOutOfBounds exception is thrown when a program tries to access an array index that is negative or greater than, or equal to the length of the array. The ArrayIndexOutOfBounds exception is a run-time exception. Java's compiler does not check for this error during compilation.
In this given program, arr.length will return the length of the array. But in Java , the index starts from 0. So, the length og the given array is 2, but the index value are 0 and 1, so it will show ArrayIndexOutOfBounds exception.
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]);
}
}
_______________________________________________________
Related Links :
In which of the following package Exception class exist?
A.java.util
B.java.file
C.java.io
D.java.lang
https://brainly.in/question/6888311
In Java exception handling, which block is generally used to clean up at the end of
Executing a try block?
a) catch
b) finally
c) throw
d) new
https://brainly.in/question/35595202
#SPJ2