+ 1
can someone help me with this question?
What is the output of this code? int arr[ ] = new int[3]; for (int i = 0; i < 3; i++) { arr[i] = i; } int res = arr[0] + arr[2]; System.out.println(res);
17 Antworten
+ 25
2
+ 10
array after declaration
arr: [ 0, 0, 0 ]
index:. 0 1 2
Loop cycle 1:. i=0
0<3 ---> true
therefore: arr[0] = 0;
Loop cycle 2:. i=1
1<3 ---> true
therefore: arr[1] = 1;
Loop cycle 3:. i=2
2<3 ---> true
therefore: arr[2] = 2;
Loop cycle 4:. i=3
3<3 ---> False
therefore:. loop ends
array after loop
arr: [ 0, 1, 2 ]
index:. 0 1 2
int res = 0 + 2;
System.out. println(2);
+ 3
2
0
2
0
2
0
2
0
2
0
2
0
2
0
0
2
0
2
0
2
0
2
0
2
0
2
0
This was hard for me, too! Thanks everyone!