Which are the values in arr after execution of following.. Int[]arr={1,1,0,0,0}; for(int i=2;i < arr.length;i++) arr[i]=arr[i-1]+arr[i-2];
-
1.11011
-
2.1121
-
3.11222
-
4.11235
-
5.11248
The loop computes Fibonacci-like values starting from index 2. Initial array: [1,1,0,0,0]. At i=2: arr[2]=arr[1]+arr[0]=1+1=2. At i=3: arr[3]=arr[2]+arr[1]=2+1=3. At i=4: arr[4]=arr[3]+arr[2]=3+2=5. Final array: [1,1,2,3,5], which as a string is 11235. Each element from index 2 onward is the sum of the two preceding elements.
To determine the values in the array arr after executing the given code, let's go through each iteration of the loop:
- Before the loop starts:
arr = [1, 1, 0, 0, 0] - Iteration 1:
arr[2] = arr[1] + arr[0], soarr[2] = 1 + 1 = 2Updatedarr = [1, 1, 2, 0, 0] - Iteration 2:
arr[3] = arr[2] + arr[1], soarr[3] = 2 + 1 = 3Updatedarr = [1, 1, 2, 3, 0] - Iteration 3:
arr[4] = arr[3] + arr[2], soarr[4] = 3 + 2 = 5Updatedarr = [1, 1, 2, 3, 5]
After executing the code, the final value of arr is [1, 1, 2, 3, 5].
Among the given options, the correct answer is D) 4.11235, as it matches the final values of the array arr.