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);?
Answers
Answered by
7
For loop runs three times as follows
when i = 0, arr[0] = 0
when i = 1, arr[1] = 1
when i = 2, arr[2] = 2
So
res = arr[0] + arr[2] = 0+2 = 2
so the output,
when i = 0, arr[0] = 0
when i = 1, arr[1] = 1
when i = 2, arr[2] = 2
So
res = arr[0] + arr[2] = 0+2 = 2
so the output,
Similar questions