Menu
Topics Index
...
`

Can Horse Kill Pawn


If you need explanation Read this topic

If you need Answer Take test on this topic

Can Horse Kill Pawn
What will be the output of the following program?
public class CanHorseKillPawn {
    static int count = 0;
    public static void main(String[] args) {
        canHorseKill('A', 2, 'B', 3);
        canHorseKill('A', 2, 'B', 4);
        canHorseKill('A', 2, 'C', 3);
        canHorseKill('B', 4, 'H', 7);
        canHorseKill('C', 3, 'A', 2);
        canHorseKill('C', 3, 'A', 4);
    }
    private static void canHorseKill(char horseRow, int horseColumn, char pawnRow, int pawnColumn) {
        boolean result = true;
        result &= (Math.abs(horseRow - pawnRow) == 2 && Math.abs(horseColumn - pawnColumn) == 1);
        result |= (Math.abs(horseRow - pawnRow) == 1 && Math.abs(horseColumn - pawnColumn) == 2 && count++ < 10);
        System.out.print(count + "" + (result ? 'K' : 'N'));
    }
}

A. 0N1N1K1N1K1K
B. 1N2K3K4N5K6K
C. 0N1K1K1N1K1K
D. Some other output or Compilation Error or Runtime 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 c.
for 1st function A-B=1 absolute value.
3-2=1.
hre count 0.result 0
so 0N.
fr 2nd func A-B 1 ,4-2 s 2 .so count 1 n result 1.
so return 1K.
similarly for othr funcs

Posted by Maheshwari Natarajan    2014-09-09 12:38:07


a.values rr passrd thru canhorse kill function..absolute values of horse row nn pawn rows r calculatd...n usng ternary oprratr the valuess of reslut r chrcked n hence ans a is printed

Posted by Nasir Khan    2014-09-09 12:43:58


C.....gr8 use of operators...(y)

Posted by Rajat Panjwani    2014-09-09 12:45:44


b.or 1st function A-B=1 absolute value.3-2=1.hre count 0.result 0so 0N.fr 2nd func A-B 1 ,4-2 s 2 .so count 1 n result 1.so return 1K.similarly for othr funcs

Posted by Asma Mujtaba Khan    2014-09-09 12:47:32


C

Posted by Pankaj Arora    2014-09-09 13:38:50


Answer c
1st func returns count0 result false
sop 0N
2 nd func count 1 result true
sop 1K
3rd func count 1 result true
sop 1K
4 th func count 1 result false
sop 1 N
5th count 1 result true
sop 1k
6th sop 1k
count s increased ly 1 tim for 2nd func.
so count remains 1 all tim aft 2nd func.

Posted by NagaRaj Nataraj    2014-09-09 14:10:52


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

Posted by Merit Campus    2014-09-10 04:20:12


© meritcampus 2019

All Rights Reserved.

Open In App