Menu
Topics Index
...
`

Kill Piracy


If you need explanation Read this topic

If you need Answer Take test on this topic

Kill Piracy
What will be the output of the following program?
public class KillPiracy {

    public static void main(String[] args) {
        args = new String[]{"lliK", "ycariP"};
        int i = 0;
        for (String arg : args) {
            process(args, i++);
            System.out.print(arg + " ");
        }
    }

    private static void process(String[] args, int i) {
        args[args.length - i - 1] = new StringBuffer(args[args.length - i - 1]).reverse().toString();
        System.out.print(args[args.length - i - 1] + " ");
    }
}

A. Piracy lliK Kill ycariP 
B. Piracy lliK Kill Piracy 
C. Kill Piracy Kill Piracy    
D. Some other output or Compilation Error or Runtime Error
Topic: Java StringBuffer, reverse() - Reverse A String 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 C as string array args consists of "llik" and "ycarip" and string is reversed in process method and prints kill piracy

Posted by Bharath Yelchuri    2014-05-26 06:10:11


Ans is B args has llik and ycarip and in process method sritngs is reversed based   on length of arguments and prints the strings

Posted by Naga Sinst    2014-05-26 06:23:46


i think it is answer d because it might be fall into compilation error because of private static void process......

Posted by Vinod Avulapati    2014-05-26 10:18:10


ans b.1st string arr and i=0 is passd to the functin process,thr args[args.1] value is reversd and assignd,insid the func prints Piracy, and main func prints llik,similarly nxt iteration llik is reversd n printd and assign value Piracy printd ib the main func

Posted by Maheshwari Natarajan    2014-05-26 14:12:11


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

Posted by Merit Campus    2014-05-28 14:51:36


© meritcampus 2019

All Rights Reserved.

Open In App