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?
Given the code fragment: What is the result?
A.
20
B.
25
C.
29
D.
Compilation fails
E.
AnArrayIndexOutOfBoundsException is thrown at runtime
I don’t feel like typing this one out. Answer is A – 20.
int[] lst = {1,2,3,4,5,4,3,2,1};
int sum=0;
for (int frnt=0, rear=lst.lenght – 1;frnt =5; frnt++, rear–) {
sum = sum + lst[frnt] + lst[rear];
}
System.out.print(sum);
1 1 = 2
2 2 = 4
3 3 = 6
4 4 = 8
A. 20
What is the exit condition for this for loop?
frnt=5 is not a conditional statement
FRNT = 5
whether frnt=5 cause compilation error
* FRNT = 5
Answer:
A
Code question:
public class Main {
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;
rnt++, rear–) {
sum = sum + lst[frnt] + lst[rear];
}
System.out.print(sum);
}
}
Output:
20