Menu
Topics Index
...
`

Track Steps


If you need explanation Read this topic

If you need Answer Take test on this topic

Track Steps
What will be the output of the following program?
class TrackSteps {
    public static void main(String[] args)
    {
        int[][] positions = {{3, 5}, {7, 8}, {1, 3}, {6, 0}, {2, 7}};
        for (int i = 0; i < positions.length - 1; i++)
        {
            int xStep = positions[i][0] > positions[i + 1][0] ? -1 : 1;
            int yStep = positions[i][1] > positions[i + 1][1] ? -1 : 1;
            int xChange = positions[i][0] - positions[i + 1][0];
            int yChange = positions[i][1] - positions[i + 1][1];
            if (Math.abs(xChange) > Math.abs(yChange))
            {
                System.out.print(Math.abs(yChange) + "[" + xStep + "," + yStep + "]" + "+" + (Math.abs(xChange) - Math.abs(yChange)) + "H ");
            }
            else
            {
                System.out.print(Math.abs(xChange) + "[" + xStep + "," + yStep + "]" + "+" + (Math.abs(yChange) - Math.abs(xChange)) + "V ");
            }
        }
    }
}

A. 3[-1,-1]+1V 5[1,1]+1V 3[-1,1]+2V 4[1,-1]+3H 
B. 3[1,-1]+1H 5[-1,1]+1H 3[1,1]+2H 4[-1,-1]+3V 
C. 3[1,1]+1H 5[-1,-1]+1H 3[1,-1]+2H 4[-1,1]+3V 
D. Some other output not shown here
E. Compilation Error or Run-time Error
Topic: Java Math Class - java.lang.Math

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 C.. when i=0 xStep=1,yStep=1 xChange=4 and yChange=3 so 4>3 goes inside of if loop.. for loop continues with other i values . and gives C option as output..

Posted by Om Prakash Om    2014-03-21 09:06:21


Congratulations Om Prakash Om. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-03-24 04:57:50


© meritcampus 2019

All Rights Reserved.

Open In App