0
i dont understand how this works
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); the answer is 2
3 Respostas
+ 4
The first line is declaring an integer array called arr of size 3(accepts 3 integer elements(
The for loop will go through each array index. It starts from index 0 up to 2. (Because 2<3)
At arr[0] you assign 0
At arr[2] you assign 2.
Result is simply 0+2 and you get 2. .
Tip : learn further how array works. Good luck.
+ 1
Happy to help. 👍
0
thx