What will be the output of the following program segment?

int ar[] = {1, 2, 3, 4, 5};
int a = 1;

for (int i = 0; i < 7; i++) { 
    a = a + i * ar[i];
}

System.out.println(a);

Single Choice
Views 89

Answer:

Array out-of-bounds issue:

The array ar has only 5 elements (ar[0] to ar[4]).

The loop runs from i = 0 to i = 6, which will attempt to access ar[5] and ar[6]. These indices are out of bounds, and the program will throw an ArrayIndexOutOfBoundsException at runtime.

Related Articles:

This section is dedicated exclusively to Questions & Answers. For an in-depth exploration of Java Programming Language, click the links and dive deeper into this subject.

Join Our telegram group to ask Questions

Click below button to join our groups.