What is the value of a[1] after the following code is executed?
int[] a = {0, 2, 4, 1, 3};
for(int i = 0; i < a.length; i++)
a[i] = a[(a[i] + 3) % a.length];
int[] a = {0, 2, 4, 1, 3};
for(int i = 0; i < a.length; i++)
a[i] = a[(a[i] + 3) % a.length];
A. 0
B. 1
C. 2
D. 3
E. 4
Answer: Option B
Join The Discussion
Comments ( 2 )
Related Questions on Flow Control
What is the purpose of the "if" statement in Java?
A. To execute a block of code conditionally
B. To create loops
C. To declare variables
D. To define methods
A. No output
B. Error
C. Hello
D. 5
for the first iteration a[0]
a [ (a[0] + 3) % a.length ] where a[0] is 0 and a.length is 5
a [ (0 + 3) % 5 ] = a[ 3 % 5 ] = a[ 3 ] = 1
here a[3] is '1' assigned to a[0]
and for the second iteration a[1]
a [ (a[1] + 3) % a.length ]
a [ (2 + 3) % 5] = a [ 5 % 5 ] = a[ 0 ] = 1
now a[0] is '1' assigned to a[1]
how