Menu
Topics Index
...
`

To Recurse is Divine


If you need explanation Read this topic

If you need Answer Take test on this topic

To Recurse is Divine
What will be the output of the following program?
public class ToRecurseIsDivine {

    public static void main(String[] args) {
        print("To Iterate Is Human");
        print("To Recurse Is Divine");
    }

    private static void print(String statement) {
        int count = statement.length();
        if (count == 0) return;
        if (count % 2 == 0) {
            System.out.print(statement.charAt(0));
            print(statement.substring(1));
        } else if (count % 2 != 0) {
            System.out.print(statement.charAt(count - 1));
            print(statement.substring(0, count - 1));
        }
    }
}

A. nTaom uIHt esrIa teTeon iRveicDu rssIe 
B. Tnoa mIutHe rsaIt eeTnoi vRieDc usrIs e
C. Some other output or throws StackOverflowError
D. Compilation Error
Topic: Java String substring() method - substring 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 A. the length of the first string passed is 19. so first if fails and character at 18th index is printed and substring from 0 with length as count-1 is passed to the method yielding T as output (length 18) and  so on

Posted by Nikhil Sulibhavi    2014-09-02 12:40:03


Ans:A  printf having 19 characters hence display first one character from last of string ,next displays first character from string. Hence it displays one character from last and star one character from the string basedon the count number

Posted by Indrasena Reddy    2014-09-02 12:41:43


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

Posted by Merit Campus    2014-09-03 04:17:20


© meritcampus 2019

All Rights Reserved.

Open In App