Menu
Topics Index
...
`

Da Vinci Code


If you need explanation Read this topic

If you need Answer Take test on this topic

Da Vinci Code
What will be the output of the following program?
public class DaVinciCode {

    static String code = "";
    static int decrement = 1;

    public static void main(String[] args) {
        code = "Mona";
        wrap('(');
        reverse();
        wrap('{');
        code += "Lisa";
        reverse();
        decrement--;
        wrap('[');
        System.out.println(reverse());
    }

    public static void wrap(char c) {
        code = c + code + (char) (c + decrement);
    }

    public static String reverse() {
        String result = code;
        code = new StringBuffer(result).reverse().toString();
        decrement++;
        return result;
    }
}

A. [{(Mona)}Lisa]
B. [asiL|(Mona){\
C. [asiL}(Mona){]
D. [asiL})anoM({]
E. Some other output or Compilation 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.

c is ans initally we are not capturing reverse method result and then next reverse on Lisa is changed to asil with the { which is wrapped before it. then we reduce the decrement which points to Mona two reverses further doesn't change the value but jsut appends { to the rsult and is printed

Posted by Karteek Paruchuri    2013-09-25 09:08:37


c

Posted by Dimple Pravallika M    2013-09-25 09:21:20


Because First reverse method can not change any value  and then next reverse can changes Lisa to asiL with  {  because of wrap. then decrement operation can works and Mona reverses twice and doesn't change  Mona but it shows { at the end.

Posted by Dimple Pravallika M    2013-09-25 09:46:36


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

Posted by Merit Campus    2013-09-26 11:44:32


© meritcampus 2019

All Rights Reserved.

Open In App