+ 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?

14th Jan 2023, 2:17 AM
Gradi Maxime Kasita Mbatika
3 Antworten
+ 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
14th Jan 2023, 3:34 AM
BroFar
BroFar - avatar
14th Jan 2023, 5:02 AM
BroFar
BroFar - avatar
+ 2
Thank you BroFar! 👍
14th Jan 2023, 5:00 AM
Gradi Maxime Kasita Mbatika