Given the code fragment:
What is the result?
A. 20
B. 25
C. 29
D. Compilation fails
E. AnArrayIndexOutOfBoundsException is thrown at runtime
Given the code fragment:
What is the result?
A. 20
B. 25
C. 29
D. Compilation fails
E. AnArrayIndexOutOfBoundsException is thrown at runtime
Option A
public class Calc {
public static void main(String[] args) {
int[] lst = {1, 2, 3, 4, 5, 4, 3, 2, 1};
int sum = 0;
for (int frnt = 0, rear = lst.length – 1;
frnt = 5;
frnt++, rear–) {
sum = sum + lst[frnt] + lst[rear];
}
System.out.print (sum);
}
}
Output:
20