ExamCompetition Forum Question Papers Ask A Question Mock Test Learn & Earn Sign Up Login Menu



23 vote

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

Asked on by | Votes 23 | Views: 5791 | Tags: computer science     | java programming     | array     | Add Bounty

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


Share on Facebook      Share on Whatsapp       Share on Twitter




1 answers

22 vote
Answered by on | Votes 22 |

 1



when i = 0;
a[i] = a[(a[i]+3)%a.length] //a.length =5;
a[0] = a[(a[0]+3)%5];
a[0] = a[(0+3)%5] ; // 3
a[0] = a[3] = 1
when i = 1;
a[1]=a[(a[1]+3)%5];
a[1]=a[(2+3)%5];
a[1]=a[0];
a[1]=1;
Therefore a[1] is equal to 1

Join Telegram Group




Answer This Question

Name:
Email:
Answer :
Sum of (1+1)
Submit: