Easy Tutorial
For Competitive Exams

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

0
1
2
3
4
Explanation:
Here's an explanation of the code:
1. The array a is initialized with values: {0, 2, 4, 1, 3}. 
2. The for loop iterates over each element of the array using the variable i. 
3. Inside the loop, the value of a[i] is updated using the expression a[(a[i] + 3) % a.length]. 
  (i) For i = 0, a[i] is 0. So, a[(0 + 3) % 5] becomes a[3 % 5] which is a[3] (value 1). 
 (ii) For i = 1, a[i] is 2. So, a[(2 + 3) % 5] becomes a[5 % 5] which is a[0] (value 0).  
 (iii) For i = 2, a[i] is 4. So, a[(4 + 3) % 5] becomes a[7 % 5] which is a[2] (value 4). 
 (iv) For i = 3, a[i] is 1. So, a[(1 + 3) % 5] becomes a[4 % 5] which is a[4] (value 3).  
 (v) For i = 4, a[i] is 3. So, a[(3 + 3) % 5] becomes a[6 % 5] which is a[1] (value 2).  
4. After the loop finishes, the modified array a becomes {0, 1, 4, 3, 2}. 
5. Finally, the code prints the value of a[1] using System.out.println(a[1]), which outputs 1.

Therefore, the output of the code is 1.
Additional Questions

When you pass an array to a method, the method receives ________ .

Answer

What is output of the following code:

public class solution{
        public static void main(String[] args){
                int[] x = {120, 200, 016 };
                for(int i = 0; i < x.length; i++)
                        System.out.print(x[i] + " ");
        }
}

Answer

Choose the correct statement. Restriction on static methods are:
I. They can only call other static methods.
II. They must only access static data.
III. They cannot refer this or super in any way.

Answer

What is output of the following program:

public class solution{
        public static void main(String[] args){
                byte x = 127;
                x++;
                x++;
               System.out.println(x);
      }
}

Answer

Select the valid statement to declare and initialize an array.

Answer

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

Answer
Share with Friends
Privacy Copyright Contact Us