Examveda
Examveda

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];

A. 0

B. 1

C. 2

D. 3

E. 4

Answer: Option B


This Question Belongs to Java Program >> Flow Control

Join The Discussion

Comments ( 2 )

  1. Teja Karri
    Teja Karri :
    1 year ago

    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]

  2. Sandeep Dagar
    Sandeep Dagar :
    6 years ago

    how

Related Questions on Flow Control