Multiple choice java core java

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. 1.11011

  2. 2.1121

  3. 3.11222

  4. 4.11235

  5. 5.11248

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

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.

AI explanation

To determine the values in the array arr after executing the given code, let's go through each iteration of the loop:

  1. Before the loop starts: arr = [1, 1, 0, 0, 0]
  2. Iteration 1: arr[2] = arr[1] + arr[0], so arr[2] = 1 + 1 = 2 Updated arr = [1, 1, 2, 0, 0]
  3. Iteration 2: arr[3] = arr[2] + arr[1], so arr[3] = 2 + 1 = 3 Updated arr = [1, 1, 2, 3, 0]
  4. Iteration 3: arr[4] = arr[3] + arr[2], so arr[4] = 3 + 2 = 5 Updated arr = [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.