Menu
Topics Index
...
`

Event test with for


If you need explanation Read this topic

If you need Answer Take test on this topic

Event test with for
What will be the output of the following program?
public class Event {
    public static void main(String[] args) {
        int order = 3;
        for (int row = 0; row < order; row++) {
            for (int col = 0; col < order; col++) {
                int rowMatrix = (((order + 1) / 2 + row + col) % order);
                int colMatrix = (((order + 1) / 2 + row + order - col - 1) % order) + 1;
                System.out.print(((rowMatrix * order) + colMatrix) + "\t");
            }
            System.out.println();
        }
    }
}

A. 1    2    3    
4    5    6    
7    8    9
B. 8    1    6    
3    5    7    
4    9    2
C. 8    1    6    
3    5    2    
4    9    7
D. Some other output
E. Compilation Error or Runtime Error
Topic: Nested for Loop In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

ans is B.. by computing we vl get 7 at 12 position in matrix.....

Posted by Om Prakash Om    2013-05-15 12:49:28


and at 22 position we vl get value of 2......

Posted by Om Prakash Om    2013-05-15 12:51:20


ans is b :as the order of preceedence is (),/,%,+,- and so on..here the value order+1 is initially calculated and is divided by 2. As both numerator and denominator are integers the value returned is an integer.nw the valu is added to row and col variable and again the mofulo operator works on the resultant value and any other addditons and substractions are done present if any outside the bracket..as the values in the brackets are calculated first...the output is 8 1 6 in 1st line 3 5 7 in 2nd line and 4 9 2 in 3rd line..

Posted by Shaik Nyamath    2013-05-15 13:28:18


answer is b..order+1
(order+1)/2 (returns int)
ans +row+col
ans%order
.......
order+1
ans/2
ans+row+order-col-1
ans%order
ans+1
is the order in which these gets executed..

Posted by Pramod Jain    2013-05-15 13:46:01


Congratulations Shaik Nyamath. You are today's daily dose winner. Please send us your mobile details and we will process the recharge tomorrow after considering the new likes.

Posted by Merit Campus    2013-05-15 14:01:10


Got RC ! :)

Posted by Shaik Nyamath    2013-05-18 06:36:38


© meritcampus 2019

All Rights Reserved.

Open In App