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

26th Aug 2020, 6:11 PM
Laurent Catudal
Laurent Catudal - avatar
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.
26th Aug 2020, 6:18 PM
shashuri Magrease
shashuri Magrease - avatar
+ 1
Happy to help. 👍
26th Aug 2020, 6:21 PM
shashuri Magrease
shashuri Magrease - avatar
0
thx
26th Aug 2020, 6:19 PM
Laurent Catudal
Laurent Catudal - avatar