Given the declarations: int[] list = new int[10]; show the elements of the array
ID: 3852215 • Letter: G
Question
Given the declarations:
int[] list = new int[10];
show the elements of the array list after the following statements are executed (exercises a, b, and c are independent. All use the same declaration.)
a) for(int i = 0; i < list.length; i++)
list[i] = 10 - i;
b) for(int i = 0; i < list.length; i++){
if (i <= 4)
list[i] = -1;
else
list[i] = 1;
}
c) for(int i = 0; i < list.length; i++){
if(i % 2 == 0)
list[i] = i;
else
list[i] = i + 1;
}
Explanation / Answer
a) for(int i = 0; i < list.length; i++)
list[i] = 10 - i;
Output:
10 9 8 7 6 5 4 3 2 1
____________________
b) for(int i = 0; i < list.length; i++){
if (i <= 4)
list[i] = -1;
else
list[i] = 1;
}
Output:
-1 -1 -1 -1 -1 1 1 1 1 1
__________________
C)
for(int i = 0; i < list.length; i++){
if(i % 2 == 0)
list[i] = i;
else
list[i] = i + 1;
}
Output:
0 2 2 4 4 6 6 8 8 10
_______________Thank Youi
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.