New course! Every coder should learn Generative AI!
Try a free lesson0
Q&A Question about arrays.
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 why this results in 2?
1 Answer
+ 1
For the entire array,
arr[i] = i;
The array content/value is the index of the array, i.e. arr[0] is 0, arr[1] is 1, and so arr[0] + arr[2] is 2.