+ 1
Why is the answer 2?
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); Can someone explain to me why the answer is 2 please?
3 Answers
+ 8
Gradi Maxime Kasita Mbatika
The explanation is in the code but the for loop is building the array arr
So
arr[0] = 0
arr[1] = 1
arr[2] = 2
So
arr[0] + arr[2] equals 2
res = 2
https://code.sololearn.com/cVI57WC7wqrn/?ref=app
+ 3
Welcome Gradi Maxime Kasita Mbatika
+ 2
Thank you BroFar! š