Matrix coding in java

Find the output of the following coding in Java:

1
2
3
4
5
6
int []a={19,4,33,7,10};
int i;

for(i=0;i<=3;++i)
a[i+1]=( a[i]%2==0 ? ++a[i] : --a[i] );
System.out.print( a[i]+",");

log on to comprogzz.in to learn computer programming

19,18,19,18,19, 19, 21,7,45,18,5 19,18,18,1,42

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

2 solutions

Connor Kenway
Apr 22, 2014

you dont even need to solve the question the last statement is printing out a single number and of all the options only one had a single number so that had to be the answer

Sandeep Thakur
Apr 21, 2014

So, the loop is from 0 to 3, and the condition inside for loop is checking current number and if its even then it sets the next number as the increment of current number otherwise decrement of current number.

So 19 is odd, so a[1] will be 18. Then loop goes to a[1], which is now 18, so it will make a[2] = 19. so a3 will become 18 again and a[4] will become 19. and loop will exit.

Printing code is not part of for loop, and last value of i will be 4.. so it will print "19,"

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...